Overloaded Infrastructure 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
ESPN

Problem

Each row of infra_nodes records a node's CPU and memory utilization, and a node is overloaded when its CPU exceeds 90% or its memory exceeds 85%. Surface each unique overloaded node's hostname, region, and node type.

CPU above 90. Memory above 80. Red alert.

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 17 rows
hostnameregionnode_type
db-replicaus-east-1compute
Api-03ap-south-1gpu
api-01us-west-2memory
Api-03eu-west-1storage
gpu-node-01eu-central-1general

Worked solution and explanation

This is a two-column OR filter dressed up as capacity alerting, with a dedup hiding under the word 'unique'. Anyone gets cpu_pct > 90 right. What separates candidates is hearing 'or' as a disjunction across two different columns with two different cutoffs, and noticing that infra_nodes records the same hostname, region and node type under more than one node_id. Read 'or' as AND and the alert list is empty on this data: no node runs hot on both at once, so every memory-saturated box with a cool CPU disappears. Skip the dedup and all 17 overloaded nodes show up twice.

Walking to the answer

Step 1: Write the definition as one predicate

cpu_pct > 90 OR mem_pct > 85. Each cutoff belongs to its own column, and both are strict: a node sitting at exactly 90.0 CPU, or exactly 85.0 memory, is not overloaded, and the table has one of each. A node at 95 memory with 65 CPU qualifies on memory alone.

Step 2: Let missing readings fall out

Some rows have no value in either cpu_pct or mem_pct. A comparison against a missing value is never true, so those rows drop out without any extra guard, which matches the business reading: no reading, no alert.

Step 3: Collapse repeated nodes

Every overloaded hostname, region and node type combination sits under two node_id values, so projecting the three columns repeats each node. DISTINCT on the projection returns each one once: 17 rows instead of 34.

OR threshold with DISTINCT
SELECT DISTINCT hostname, region, node_type
FROM infra_nodes
WHERE cpu_pct > 90 OR mem_pct > 85
Both metrics hot
WHERE cpu_pct > 90 AND mem_pct > 85 demands both at once. On this table no node qualifies, so the alert list is empty.
Either metric hot
WHERE cpu_pct > 90 OR mem_pct > 85 flags 17 unique nodes, including 'db-replica' at 95% memory and 65% CPU.

Common follow-up questions