Lowest CPU Pods per Namespace

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

Domain
SQL
Difficulty
hard
Seniority
L4
Asked in
City and County of San Francisco

Problem

For each Kubernetes namespace, surface the five pods with the lowest CPU usage, treating a pod with no recorded CPU as the lowest. Pods sharing a CPU value take the same position with no gap after them, so a namespace with a tie at the cutoff can return more than five rows. Show each pod's name, namespace, CPU, and memory usage, grouped by namespace with the lowest usage first and ties broken by pod id.

The five lightest pods per namespace.

Tables

k8s_pods

Columns: pod_id INTEGER, pod_name TEXT, nspace TEXT, status TEXT, restarts INTEGER, cpu_used REAL, mem_used REAL

k8s_pods: 10 of 200 rows
pod_idpod_namenspacestatusrestartscpu_usedmem_used
2043payment-003efproductionPending00.01137
2086search-003f6stagingCrashLoopBackOff00.02274
2129gateway-003fdmonitoringCompleted00.033111
2172worker-00404kube-systemError00.044148
2215cron-0040bdefaultTerminating00.055185
2258ml-serve-00412productionRunning0NULLNULL
2301cache-00419stagingPending00.077259
2344auth-00420monitoringCrashLoopBackOff00.088296
2387payment-00427kube-systemCompleted00.099333
2430search-0042edefaultError00.11370

Expected output

First 5 of 72 rows
pod_namenspacecpu_usedmem_used
cron-0040bdefault0.055185
auth-00490kube-systemNULLNULL
search-00466monitoringNULLNULL
ml-serve-00412productionNULLNULL
worker-0043cstagingNULLNULL

Worked solution and explanation

Why this problem exists in real interviews

This is a per-group top-N hiding inside a capacity-planning ask: keep the five lightest pods in every one of ~86k namespaces, in a single pass over 3M rows. Anyone can sort and cap; the separator is picking the window that keeps tied pods together at the boundary and then making the final order total so a correct answer never grades as wrong on a tie.


Break down the requirements

Step 1: Rank within each namespace

Partition by nspace and order by cpu_used ascending. Nulls sort first in ascending order, which is exactly what we want: a pod with no recorded CPU is treated as the lowest and stays in contention for the five.

Step 2: Rank with DENSE_RANK for tie inclusion

DENSE_RANK() assigns the same rank to tied CPU values and never skips a number, so filtering to rnk <= 5 keeps a full band of five usage levels. When the fifth level is tied, every pod at that level survives, so a namespace can legitimately return more than five rows.

Step 3: Make the final order total

Order the output by nspace, then rnk, then pod_id. The first two group each namespace's pods lowest-CPU first, but they are not unique when pods tie, so pod_id (unique per pod) makes the sort TOTAL: no two rows share every sort key, and a correct answer can never lose to arbitrary engine order.


The solution

`DENSE_RANK` per namespace for tie-inclusive top-N
SELECT pod_name, nspace, cpu_used, mem_used
FROM (
    SELECT pod_name, nspace, cpu_used, mem_used, pod_id,
        DENSE_RANK() OVER (PARTITION BY nspace ORDER BY cpu_used ASC) AS rnk
    FROM k8s_pods
) ranked
WHERE rnk <= 5

Common follow-up questions