The High and the Low

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
KPMG

Problem

Which services in svc_health posted the highest and the lowest latency in each US region, labeled 'highest' or 'lowest' by the end they sit at? The on-call engineer reviewing them wants every service tied at an end, not just one of them.

The fastest and slowest in every region.

Tables

svc_health

Columns: check_id INTEGER, svc_name TEXT, status TEXT, latency REAL, uptime REAL, checked TEXT, region TEXT

svc_health: 10 of 200 rows
check_idsvc_namestatuslatencyuptimecheckedregion
137payment-apidegraded3.697.032026-02-02 01:05:00us-west-2
174user-svcunhealthy6.794.062026-03-03 02:10:00eu-west-1
211search-apitimeout9.891.092026-04-04 03:15:00ap-south-1
248gatewayHealthy12.998.122026-05-05 04:20:00eu-central-1
285notif-svcDEGRADED1695.152026-06-06 05:25:00us-east-1
322ml-servinghealthy19.192.182026-07-07 06:30:00us-west-2
359cache-svcdegradedNULL99.212026-08-08 07:35:00eu-west-1
396queue-workerunhealthy25.396.242026-09-09 08:40:00ap-south-1
433cron-svctimeout28.493.272026-01-10 09:45:00eu-central-1
470auth-svcHealthy31.590.32026-02-11 10:50:00us-east-1

Expected output

First 4 of 4 rows
svc_nameregionlatencylatency_type
queue-workerus-east-1808.7highest
notif-svcus-east-116lowest
gatewayus-west-2792.3highest
payment-apius-west-23.6lowest

Worked solution and explanation

What this problem really is

This is a two-headed extremes query wearing an SRE dashboard costume. The real skill: can you pull both the slowest and the fastest service per region in a single pass, and keep every service that ties at either edge? Anyone can sort and grab the top row. The trap is ties: reach for ROW_NUMBER and a region with three services at the same latency loses two of them. The second trap is the filters. The non-US regions and the NULL latency rows have to go before you rank, or a NULL sorts to the front of the ascending order and quietly steals the 'lowest' slot.


Break down the requirements

Step 1: Scope to US regions and measured checks

Keep only rows of svc_health whose region starts with 'us-', and drop rows where latency is NULL. A NULL latency is not a measurement, and SQLite sorts it first under an ascending order, so leaving it in hands it the low end.

Step 2: DENSE_RANK twice in the same SELECT

In a CTE, compute two DENSE_RANK() windows per region: one ordering latency DESC (high_rank) and one ordering latency ASC (low_rank). Tied extremes all share rank 1, which is the whole point.

Step 3: Keep the ends and label them

Keep rows where high_rank = 1 or low_rank = 1, and let a CASE turn the ranks into the 'highest' or 'lowest' label. No final sort: the question asks which services, not in what order.


The solution

Double rank, then filter to the extremes
WITH ranked AS (
  SELECT svc_name, region, latency,
         DENSE_RANK() OVER (PARTITION BY region ORDER BY latency DESC) AS high_rank,
         DENSE_RANK() OVER (PARTITION BY region ORDER BY latency ASC)  AS low_rank
  FROM svc_health
  WHERE region LIKE 'us-%'
    AND latency IS NOT NULL
)
SELECT svc_name, region, latency,
       CASE WHEN high_rank = 1 THEN 'highest' WHEN low_rank = 1 THEN 'lowest' END AS latency_type
FROM ranked
WHERE high_rank = 1 OR low_rank = 1

Common follow-up questions