The Footprint

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

Domain
SQL
Difficulty
medium
Seniority
L3
Asked in
Forbes

Problem

Capacity planning is reviewing how the server fleet spreads across regions. Show the node count for each region.

Some regions carry more of the fleet than others. Show how it is spread.

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 6 rows
regionnode_count
ap-south-139
eu-central-140
eu-west-140
sa-east-11
us-east-140

Worked solution and explanation

What this is really testing

Underneath the capacity-planning framing this is a plain group-and-tally: one row per region, COUNT(*) as the metric, sorted so the heaviest region floats to the top. Anyone can write the GROUP BY. What separates candidates is making the sort deterministic. When two regions carry the same node count, a bare ORDER BY node_count DESC leaves their relative order to the engine, so the row you call 'first' can shift between runs. Add a tiebreaker and the output is stable.


Building it

Step 1: Collapse to one row per region

GROUP BY region collapses the 10,000 node rows into one row per region. Every non-aggregated column in the SELECT has to be the grouping key, which is why only region and the aggregate appear.

Step 2: Count every node

COUNT(*) tallies every node in each region, including stopped, draining, and offline ones. The prompt asks for the fleet's spread, not just the live nodes, so there is no status filter. If it asked for active nodes only, you would move that condition into a WHERE clause before grouping.

Step 3: Sort heaviest first, deterministically

ORDER BY node_count DESC puts the densest region first. Append region as a secondary sort so regions tied on count come back in a fixed order rather than an arbitrary one.


The solution

Node count per region, densest first
SELECT region, COUNT(*) AS node_count
FROM infra_nodes
GROUP BY region

One scan, one grouped aggregate, one sort.

A single fact table; region is the grouping dimension.
  • infra_nodes: node_id int PK, hostname text, region text, node_type text, cpu_pct float, mem_pct float, status text

Common follow-up questions