Endpoint Latency Spread

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
City and County of San Francisco

Problem

Only a 2xx status in api_calls marks a call that succeeded: errors fail instantly or hang until a timeout, and redirects never exercise the endpoint's real work, so neither says how the service performs. From the successful calls, report each endpoint's slowest and fastest latency, the gap between them, and how many whole times slower its slowest call is than its fastest, as an integer with any fraction dropped (a 7.9x slowdown counts as 7).

Latency spread across endpoints.

Tables

api_calls

Columns: call_id INTEGER, endpoint TEXT, method TEXT, status INTEGER, latency REAL, user_id INTEGER, call_time TEXT, err_msg TEXT

api_calls: 10 of 670 rows
call_idendpointmethodstatuslatencyuser_idcall_timeerr_msg
8079/api/v1/ordersPOST2004.21002026-02-02 01:07:00NULL
8158/api/v2/productsPUT2007.91972026-03-03 02:14:00NULL
8237/api/v1/searchDELETE20111.62942026-04-04 03:21:00NULL
8316/api/v1/auth/loginPATCH20415.33912026-05-05 04:28:00NULL
8395/api/v1/auth/logoutget30119NULL2026-06-06 05:35:00NULL
8474/api/v2/analyticspost40022.75852026-07-07 06:42:00Not found
8553/api/v1/paymentsGET40126.46822026-08-08 07:49:00Unauthorized
8632/api/v1/users/POST40330.17792026-09-09 08:56:00Rate limit exceeded
8711/api/v2/products/PUT40433.88762026-01-10 09:03:00Internal server error
8790/api/v1/usersDELETE50037.5NULL2026-02-11 10:10:00Bad gateway

Expected output

First 5 of 10 rows
endpointlatency_difflatency_ratiomax_latencymin_latency
/api/v1/auth/login1617.21621627.210
/api/v1/auth/logout1634.51641644.510
/api/v1/orders1571.13751575.34.2
/api/v1/payments203.45248.445
/api/v1/search1599.91601609.910

Worked solution and explanation

What this really tests

This is a per-group spread calculation wearing an observability costume, and the real skill is getting the scope and the output type right: extremes per endpoint, over successful calls only, then a slowdown reported as a whole multiple. Everyone writes MAX(latency) and MIN(latency). Leave the error rows in and a timed-out 5xx becomes an endpoint's slowest call, so the gap measures the failure path. Divide the two extremes and stop there and you hand back 7.83 where the ask was 7: latencies are fractional, so the quotient is too. The conversion to an integer has to wrap the finished division.


Break down the requirements

Step 1: Scope to successful calls

Errors fail fast or hang to a timeout, so their latencies describe the failure path, not the service. Filter with status < 300 before aggregating, so the extremes come only from calls that did their job.

Step 2: Take the extremes once per endpoint

Collapse each endpoint to its MAX(latency) and MIN(latency) with GROUP BY endpoint. The gap and the multiple are plain arithmetic on those same two aggregates in the same SELECT, so the table is scanned once.

Step 3: Derive the gap and the whole multiple

The gap is MAX(latency) - MIN(latency). For the multiple, divide the two extremes first, then wrap the whole quotient in CAST(... AS INTEGER): the conversion drops the fraction, so a slowest call 7.9 times the fastest reports 7. Converting only one operand changes nothing, because the other one is still fractional.

The solution

Per-endpoint latency spread over successful calls
SELECT endpoint,
       MAX(latency) - MIN(latency) AS latency_diff,
       CAST(MAX(latency) / MIN(latency) AS INTEGER) AS latency_ratio,
       MAX(latency) AS max_latency,
       MIN(latency) AS min_latency
FROM api_calls
WHERE status < 300
GROUP BY endpoint

Common follow-up questions