Average GPU Node CPU Usage

An easy SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
easy
Seniority
L3
Asked in
ESPN

Problem

Chasing throttled ML training jobs, an on-call engineer wants to check whether the GPU nodes in infra_nodes are running hot, and a node that never reported a CPU reading tells her nothing either way. What is the average CPU percentage across the nodes typed 'gpu'?

GPU nodes burning CPU. How much?

Tables

infra_nodes

Columns: node_id INTEGER, hostname TEXT, region TEXT, node_type TEXT, cpu_pct REAL, mem_pct REAL, status TEXT

infra_nodes: 10 of 200 rows
node_idhostnameregionnode_typecpu_pctmem_pctstatus
1037web-02us-west-2memory1319stopped
1074api-01eu-west-1storage2638draining
1111api-02sa-east-1gpu3957offline
1148db-primaryeu-central-1general5276Running
1185db-replicaus-east-1compute6595STOPPED
1222cache-01us-west-2memory7814running
1259worker-01eu-west-1StorageNULLNULLstopped
1296worker-02ap-south-1gpu452draining
1333gpu-node-01eu-central-1general1771offline
1370WEB-03us-east-1compute3090Running

Expected output

First 1 of 1 rows
avg_cpu_pct
51.35294117647059

Worked solution and explanation

Why this problem exists in real interviews

This is a subgroup average dressed up as a capacity investigation. Anyone can type AVG(cpu_pct). What separates candidates is what they say before typing: the average has to be scoped to the gpu nodes, and the six gpu nodes that never reported a reading have to fall out rather than count as zero. Average the whole of infra_nodes and you report the fleet. Zero-fill the gaps and you report 43.65 instead of 51.35, and the team misreads how hot the GPU tier is.


Break down the requirements

Step 1: Pin the grain

Ask: is infra_nodes one row per node, or one snapshot per node over time? If snapshots, your average mixes nodes and time. Here node_id is unique across the 200 rows, so it is one row per node; say that you checked.

Step 2: Filter on node_type

WHERE node_type = 'gpu' is the cohort. Look at the values before trusting the literal: node_type does carry a 'Storage' case variant, but every gpu node is spelled 'gpu', so the plain equality catches all 40.

Step 3: AVG handles NULL on its own

AVG(cpu_pct) skips NULL rows in both numerator and denominator, so 34 readings are averaged, not 40. No COALESCE, no WHERE cpu_pct IS NOT NULL. Flag the assumption out loud: missing telemetry is excluded, not zeroed.

Step 4: Alias the scalar

Return AVG(cpu_pct) AS avg_cpu_pct. Single scalar, one row out. No GROUP BY needed when every selected expression is an aggregate.


The solution

GPU NODE AVERAGE CPU
SELECT AVG(cpu_pct) AS avg_cpu_pct
FROM infra_nodes
WHERE node_type = 'gpu';

COMMON FOLLOW-UP QUESTIONS

Common follow-up questions