Average Node Utilization

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
Airbnb

Problem

The quarterly capacity review is tomorrow and the VP wants a utilization heatmap by region and node type. Show average CPU and memory utilization for each combination so the team can spot over- and under-provisioned segments.

CPU and memory, region by region.

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 7 rows
regionnode_typeavg_cpu_pctavg_mem_pct
ap-south-1gpu51.7272727272727346.696969696969695
eu-central-1general45.823529411764745.705882352941174
eu-west-1StorageNULLNULL
eu-west-1storage48.9411764705882348
sa-east-1gpu3957

Worked solution and explanation

What this is really asking

infra_nodes carries cpu_pct and mem_pct per host, and the VP wants one cell per (region, node_type). The whole problem is grouping by both keys at once, not separately.


Break down the requirements

Step 1: Group by the composite key

GROUP BY region, node_type creates one row per cell of the heatmap. Grouping by either column alone collapses the other dimension and the VP loses the segment view.

Step 2: Two averages, one pass

AVG(cpu_pct) AS avg_cpu_pct and AVG(mem_pct) AS avg_mem_pct in the same SELECT. Both aggregates share the same group, so this is a single scan over 10k rows.


The solution

AVG UTILIZATION BY REGION AND TYPE
SELECT
    region,
    node_type,
    AVG(cpu_pct) AS avg_cpu_pct,
    AVG(mem_pct) AS avg_mem_pct
FROM infra_nodes
GROUP BY region, node_type

COMMON FOLLOW-UP QUESTIONS

Common follow-up questions