Mid-CPU Nodes

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
Amazon

Problem

A headroom list that should hold every node running between 40 and 80 percent CPU inclusive came back short for the capacity team's holiday traffic freeze review. Rebuild it from infra_nodes, with every field of each qualifying node.

Not the heaviest. Not the lightest. The middle.

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 5 of 72 rows
node_idhostnameregionnode_typecpu_pctmem_pctstatus
1148db-primaryeu-central-1general5276Running
1185db-replicaus-east-1compute6595STOPPED
1222cache-01us-west-2memory7814running
1444web-01eu-west-1storage5628running
1481web-02ap-south-1gpu6947stopped

Worked solution and explanation

This is a closed-interval filter dressed up as capacity planning. Everyone writes a range test on cpu_pct. What separates candidates is whether the word 'inclusive' survives into the predicate. The seed has nodes sitting at exactly 40.0 and exactly 80.0 CPU, so cpu_pct > 40 AND cpu_pct < 80 quietly drops them, and the headroom list comes back four nodes short of the 72 that qualify, the night before the freeze.

Walking to the answer

Step 1: Return the whole row from infra_nodes

'Every field' means all seven columns, node_id through status. Name them instead of writing SELECT * so the output shape stays pinned if someone adds a column to the table later.

Step 2: Keep both ends of the band

BETWEEN 40 AND 80 is inclusive on both sides by definition: it means exactly cpu_pct >= 40 AND cpu_pct <= 80. The nodes reading 40.0 and 80.0 are the boundary rows that the strict version loses, and they are the whole point of the problem.

Step 3: Let the missing readings fall out

28 nodes have no cpu_pct. A comparison against NULL is unknown rather than true, so WHERE discards those rows with no extra clause. That is the right call here, because a node with no reading cannot be placed inside the band.

Closed band on `cpu_pct`
SELECT node_id, hostname, region, node_type, cpu_pct, mem_pct, status
FROM infra_nodes
WHERE cpu_pct BETWEEN 40 AND 80
Strict bounds
cpu_pct > 40 AND cpu_pct < 80 returns 68 rows. The nodes at exactly 40.0 and 80.0 vanish with no error and no warning, and the list simply looks a little thin.
Closed band
cpu_pct BETWEEN 40 AND 80 returns all 72 rows, which is identical to cpu_pct >= 40 AND cpu_pct <= 80. Pick whichever reads clearer; the plan is the same.

Common follow-up questions