Multi-Host Regions by Node Type

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
Google

Problem

A region with only one or two compute, storage, network or GPU nodes is too thin for the capacity analyst's density review, and a hostname that sits on several of those nodes still counts as one host. Using infra_nodes, list each region with more than two such nodes and the number of unique hostnames behind them.

Some regions are quietly building empires.

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 3 of 3 rows
regionunique_hostnames
ap-south-112
eu-west-112
us-east-112

Worked solution and explanation

The trap

This is a rows-versus-identities problem wearing a capacity-planning badge. infra_nodes holds one row per provisioned node, and the same hostname is reused across many of them: a region with forty nodes can be running on eight names. Everyone filters the node types and groups by region. What separates candidates is keeping two counts straight: the thin-region bar is about nodes, the reported figure is about hosts. Let the reported figure count rows and every busy region shows its node count as its host count, inflated several times over.

Build it

Step 1: Keep only the four node types

Filter node_type with IN ('compute', 'storage', 'network', 'gpu') in the WHERE. 'memory' and 'general' nodes are out of scope, and dropping them before grouping keeps them out of both counts.

Step 2: Group by region alone

The answer is one row per region, so region is the only key. Adding node_type would split a region across rows and count a hostname serving two types twice.

Step 3: Count hosts, not rows

COUNT(DISTINCT hostname) collapses every reuse of a name into one host. COUNT(hostname) looks identical at a glance and quietly returns the node count under a host label.

Step 4: Gate on node volume after grouping

The thin-region rule is about nodes, so HAVING COUNT(*) > 2 tests rows per region once they are grouped. It cannot sit in WHERE, which only ever sees one node at a time.

Two counts, two questions
SELECT
    region,
    COUNT(DISTINCT hostname) AS unique_hostnames
FROM infra_nodes
WHERE node_type IN ('compute', 'storage', 'network', 'gpu')
GROUP BY region
HAVING COUNT(*) > 2

The gate counts nodes; the reported column counts the names those nodes run under.

Counting rows
COUNT(hostname) adds one per node. A region whose nodes reuse eight names across forty rows reports 40 hosts.
Counting hosts
COUNT(DISTINCT hostname) adds one per name. The same region reports 8, the number of hosts capacity planning actually has to size.

Common follow-up questions