The Steady Few
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
- L4
- Asked in
- Capital One
Problem
Regions whose probes sometimes come back without a latency reading keep sliding down our reliability leaderboard, yet a check with no recorded latency should count as zero latency, and only a check with no recorded uptime has nothing to add. In svc_health, a check's effective hours is its uptime minus a tenth of its latency, and a region's score is the total of those, rounded to two decimals. Return the regions holding the top three places, most reliable first, with equal totals sharing a place.
Endurance is measured in the hours you hold the line. Rank the regions that keep it.
Tables
svc_health
Columns: check_id INTEGER, svc_name TEXT, status TEXT, latency REAL, uptime REAL, checked TEXT, region TEXT
| check_id | svc_name | status | latency | uptime | checked | region |
|---|---|---|---|---|---|---|
| 137 | payment-api | degraded | 3.6 | 97.03 | 2026-02-02 01:05:00 | us-west-2 |
| 174 | user-svc | unhealthy | 6.7 | 94.06 | 2026-03-03 02:10:00 | eu-west-1 |
| 211 | search-api | timeout | 9.8 | 91.09 | 2026-04-04 03:15:00 | ap-south-1 |
| 248 | gateway | Healthy | 12.9 | 98.12 | 2026-05-05 04:20:00 | eu-central-1 |
| 285 | notif-svc | DEGRADED | 16 | 95.15 | 2026-06-06 05:25:00 | us-east-1 |
| 322 | ml-serving | healthy | 19.1 | 92.18 | 2026-07-07 06:30:00 | us-west-2 |
| 359 | cache-svc | degraded | NULL | 99.21 | 2026-08-08 07:35:00 | eu-west-1 |
| 396 | queue-worker | unhealthy | 25.3 | 96.24 | 2026-09-09 08:40:00 | ap-south-1 |
| 433 | cron-svc | timeout | 28.4 | 93.27 | 2026-01-10 09:45:00 | eu-central-1 |
| 470 | auth-svc | Healthy | 31.5 | 90.3 | 2026-02-11 10:50:00 | us-east-1 |
Expected output
| region | total_effective_hours |
|---|---|
| eu-central-1 | 2393.13 |
| us-west-2 | 2388.9 |
| eu-west-1 | 2371.74 |
Worked solution and explanation
What this problem really is
This is a NULL-safe sum dressed up as an SRE leaderboard, with a top three that counts places instead of rows. Everyone writes SUM(uptime - latency / 10.0) and groups by region. The trap is the check with no latency reading: the whole per-row term becomes NULL, SUM skips it, and the region loses that check's entire uptime, not just its latency penalty. On this seed that is 23 of 200 checks, four or five per region, each worth 90 to 100 hours, so every total on the board comes back low. The quieter half is the cut: 'top three places, equal totals sharing a place' is a statement about values, and a bare LIMIT 3 is only right while nobody ties for third.
Reading the requirements
Step 1: Score each check without losing it
A check's effective hours is uptime - latency / 10.0. When latency is NULL the subtraction is NULL, so wrap it as COALESCE(latency, 0) and the check keeps its full uptime. A check with no uptime stays NULL and SUM ignores it, which is what the ask wants: there is nothing to score.
Step 2: Total per region, round once at the end
Sum the per-check terms in one GROUP BY region pass. Keep the totals unrounded while choosing places, and apply ROUND(..., 2) only in the final projection, so the rounding cannot create or break a tie.
Step 3: Keep three places, then present them
DENSE_RANK() over the descending totals gives equal totals the same place, and rnk <= 3 keeps every region in places one to three. The filter chooses the rows but does not sort them, so a trailing ORDER BY total_effective_hours DESC lists them most reliable first.
The solution
WITH effective AS (
SELECT region,
SUM(uptime - COALESCE(latency, 0) / 10.0) AS total_effective_hours
FROM svc_health
GROUP BY region
),
ranked AS (
SELECT region,
total_effective_hours,
DENSE_RANK() OVER (ORDER BY total_effective_hours DESC) AS rnk
FROM effective
)
SELECT region, ROUND(total_effective_hours, 2) AS total_effective_hours
FROM ranked
WHERE rnk <= 3
ORDER BY total_effective_hours DESCCommon follow-up questions
Common follow-up questions
- How would you make this incremental so the dashboard refreshes hourly without rescanning 30M rows? (Probes a rollup keyed on region and hour of `checked`, and a merge strategy for late checks.)
- Is crediting a check with no `latency` its full uptime the right call, or should a timeout be penalized? (Tests whether you can defend the `COALESCE` choice and price an alternative, such as a fixed penalty.)
- What changes if the leaderboard must show the place number next to each region? (Checks that you would return `rnk` and explain why `DENSE_RANK` and `RANK` print different numbers after a tie.)