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

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 3 of 3 rows
regiontotal_effective_hours
eu-central-12393.13
us-west-22388.9
eu-west-12371.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

Top three places by effective hours
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 DESC

Common follow-up questions

Common follow-up questions