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
| call_id | endpoint | method | status | latency | user_id | call_time | err_msg |
|---|---|---|---|---|---|---|---|
| 8079 | /api/v1/orders | POST | 200 | 4.2 | 100 | 2026-02-02 01:07:00 | NULL |
| 8158 | /api/v2/products | PUT | 200 | 7.9 | 197 | 2026-03-03 02:14:00 | NULL |
| 8237 | /api/v1/search | DELETE | 201 | 11.6 | 294 | 2026-04-04 03:21:00 | NULL |
| 8316 | /api/v1/auth/login | PATCH | 204 | 15.3 | 391 | 2026-05-05 04:28:00 | NULL |
| 8395 | /api/v1/auth/logout | get | 301 | 19 | NULL | 2026-06-06 05:35:00 | NULL |
| 8474 | /api/v2/analytics | post | 400 | 22.7 | 585 | 2026-07-07 06:42:00 | Not found |
| 8553 | /api/v1/payments | GET | 401 | 26.4 | 682 | 2026-08-08 07:49:00 | Unauthorized |
| 8632 | /api/v1/users/ | POST | 403 | 30.1 | 779 | 2026-09-09 08:56:00 | Rate limit exceeded |
| 8711 | /api/v2/products/ | PUT | 404 | 33.8 | 876 | 2026-01-10 09:03:00 | Internal server error |
| 8790 | /api/v1/users | DELETE | 500 | 37.5 | NULL | 2026-02-11 10:10:00 | Bad gateway |
Expected output
| endpoint | latency_diff | latency_ratio | max_latency | min_latency |
|---|---|---|---|---|
| /api/v1/auth/login | 1617.2 | 162 | 1627.2 | 10 |
| /api/v1/auth/logout | 1634.5 | 164 | 1644.5 | 10 |
| /api/v1/orders | 1571.1 | 375 | 1575.3 | 4.2 |
| /api/v1/payments | 203.4 | 5 | 248.4 | 45 |
| /api/v1/search | 1599.9 | 160 | 1609.9 | 10 |
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
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 endpointCommon follow-up questions
- How would you compute p95 minus p50 instead, to be robust to outliers? (Tests percentile functions such as `PERCENTILE_CONT`.)
- What if `min_latency` can be 0 for a cached response? (Tests guarding a division against a zero denominator.)
- How would you compute this spread over a rolling 1-hour window of `call_time`? (Tests time-bucketing and window framing.)