The Usual Suspects

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
Vesta Innovations

Problem

Timed-out health checks keep paging the on-call engineer, and she wants to know which service is behind them in each cloud region. From svc_health, list every service that logged the most timeouts in its region, co-leaders included, with that count, the calendar month it first timed out, and how many services in the region logged a timeout at all.

Same services, same checks, same problems.

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 5 of 8 rows
regionsvc_nametimeout_countfirst_timeout_monthservices_with_timeouts
ap-south-1search-api42026-012
eu-central-1cron-svc42026-012
eu-west-1auth-svc32026-022
us-east-1notif-svc32026-012
us-west-2gateway32025-022

Worked solution and explanation

What this problem really is

Strip the monitoring costume off and this is a per-region argmax with ties: for each region, which service logged the most timed-out checks. The skill it probes is scoping the maximum to the group instead of the whole table. Anyone can count timeouts per service. The tell is whether your MAX is correlated to the region or applied globally. A global MAX compares every region against the single top count of 4, so only the two regions that reach it come back and the other three, each with co-leaders at 3, vanish. The two extra asks, the month each leader first timed out and how many services in the region timed out at all, ride for free on the same grouped set.


Working through it

Step 1: Filter to the timed-out checks

Keep only the timed-out checks with WHERE status = 'timeout'. The status column has mixed casing on the other outcomes ('Healthy'/'healthy', 'DEGRADED'/'degraded'), which is a distractor here: 'timeout' is always written in lower case, so a plain equality is exact. Putting the predicate in WHERE also lets it run before aggregation, cutting the scan down first.

Step 2: Count timeouts per service within each region

GROUP BY region, svc_name with COUNT(*) AS timeout_count gives one row per service per region. In the same pass, strftime('%Y-%m', MIN(checked)) records the calendar month that service first timed out. Materialize this in a CTE named regional_counts so you can lean on it three times: the rows to keep, the source of each region's maximum, and the self-join peer set.

Step 3: Correlate the maximum to the region

The comparison must be scoped to the same region: WHERE timeout_count = (SELECT MAX(timeout_count) FROM regional_counts rc2 WHERE rc2.region = rc.region). The correlation on rc2.region = rc.region is the whole game. Drop it and you compare every region against one global maximum. Because it is equality, every service tied at a region's top count survives, so co-leaders both come back.

Step 4: Count the region's other offenders with a self-join

JOIN regional_counts peer ON peer.region = rc.region fans each surviving leader out to every service in its region, and COUNT(peer.svc_name) after the GROUP BY collapses that back into how many services in the region logged any timeout. The self-join reads the already-tiny CTE, not the base table, so it costs almost nothing.


The solution

CTE, a region-correlated MAX subquery that keeps ties, and a self-join for the peer count
WITH regional_counts AS (
  SELECT
    region,
    svc_name,
    COUNT(*) AS timeout_count,
    strftime('%Y-%m', MIN(checked)) AS first_timeout_month
  FROM svc_health
  WHERE status = 'timeout'
  GROUP BY region, svc_name
)
SELECT
  rc.region,
  rc.svc_name,
  rc.timeout_count,
  rc.first_timeout_month,
  COUNT(peer.svc_name) AS services_with_timeouts
FROM regional_counts rc
JOIN regional_counts peer
  ON peer.region = rc.region
WHERE rc.timeout_count = (
  SELECT MAX(rc2.timeout_count)
  FROM regional_counts rc2
  WHERE rc2.region = rc.region
)
GROUP BY rc.region, rc.svc_name, rc.timeout_count, rc.first_timeout_month

Common follow-up questions