Healthiest Service Check History

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

We run scheduled health checks across a fleet of services, and every check logs an uptime score. Find the service or services whose uptime ever reached the highest value recorded anywhere in the table, and take that service's entire check history, not just its top-scoring checks. For every one of its checks show the check time, the time of the immediately preceding check, and the number of days between the two, and because the earliest check has nothing before it, keep that row too with its previous time and its gap left blank.

The healthiest service. Full history.

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 18 rows
svc_namecheckedprev_checkeddays_diff
cache-svc2023-01-09 12:12:00NULLNULL
cache-svc2023-03-11 02:02:002023-01-09 12:12:0060.57638888899237
cache-svc2023-05-01 04:52:002023-03-11 02:02:0051.118055555503815
cache-svc2023-05-13 16:52:002023-05-01 04:52:0012.5
cache-svc2023-07-03 18:42:002023-05-13 16:52:0051.07638888899237

Worked solution and explanation

This is a gap-between-consecutive-rows problem hiding behind a service-health leaderboard. There are two moves. First, a global aggregate picks the group: the single highest uptime ever recorded, and every service that touched it. Second, an ordered window walks each chosen service's full timeline. Most candidates get the LAG roughly right. The failure is almost always in the first move. ORDER BY uptime DESC LIMIT 1 quietly keeps one co-champion and deletes the other's entire history, so whole blocks of expected rows vanish and nothing errors.

The trap

Looks right
ORDER BY uptime DESC LIMIT 1 returns one row, so one service. A per-service MAX(uptime) goes wrong the other way and admits every service. Subtracting DATE(checked) values turns a gap of 79.59 days into 79 or 80.
Actually right
uptime = (SELECT MAX(uptime) FROM svc_health) keeps every service tied at the global max. JULIANDAY on the full checked timestamp keeps the fractional day.

Building it

Step 1: Gate on the global max, ties included

The scalar subquery (SELECT MAX(uptime) FROM svc_health) is computed once over the whole table. Filtering on equality with it and applying SELECT DISTINCT svc_name gives a small set of names in healthiest. If two services share the top score, both appear in that set.

Step 2: Take the whole history, not the winning rows

The outer query filters on s.svc_name IN (SELECT svc_name FROM healthiest) and does not filter on uptime. The prompt asks for every check the champion ever logged. Filtering on uptime a second time would shrink each history to its top-scoring checks.

Step 3: Walk each timeline with LAG

LAG(s.checked) OVER (PARTITION BY s.svc_name ORDER BY s.checked) returns the check immediately before the current one within the same service. The query contains two LAG calls. Because both use an identical window, the engine sorts once and evaluates the window a single time.

Step 4: Let the first check stay NULL

The earliest check in each partition has nothing before it, so LAG returns NULL and JULIANDAY(NULL) also yields NULL. The row stays in the output with blank prev_checked and days_diff, which is what the prompt asks for. Adding WHERE prev_checked IS NOT NULL would drop one correct row per service.

Full history of every top-uptime service
WITH healthiest AS (
    SELECT DISTINCT svc_name
    FROM svc_health
    WHERE uptime = (SELECT MAX(uptime) FROM svc_health)
)
SELECT
    s.svc_name,
    s.checked,
    LAG(s.checked) OVER (PARTITION BY s.svc_name ORDER BY s.checked) AS prev_checked,
    JULIANDAY(s.checked)
    - JULIANDAY(LAG(s.checked) OVER (PARTITION BY s.svc_name ORDER BY s.checked)) AS days_diff
FROM svc_health s
WHERE s.svc_name IN (SELECT svc_name FROM healthiest)

One global MAX, one shared window, fractional-day gaps.

Common follow-up questions