Top 2 Busiest API Slots
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
- Travelport
Problem
A reliability team is triaging which failures crowd the day, counting only calls that came back with an error message: hours before 12 are Morning, 12 through 15 are Early Afternoon, and anything later is Late Afternoon. The slots run different lengths, so for every HTTP status measure each slot by its failures per hour, and return the status-and-slot pairs whose rate is among the 4 highest, keeping every pair tied at a rate.
2 time slots per week. The busiest.
Tables
api_calls
Columns: call_id INTEGER, endpoint TEXT, method TEXT, status INTEGER, latency REAL, user_id INTEGER, call_time TIMESTAMP, 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
| status | time_segment | call_count | failures_per_hour |
|---|---|---|---|
| 500 | Early Afternoon | 4 | 1 |
| 401 | Morning | 7 | 0.5833333333333334 |
| 403 | Early Afternoon | 2 | 0.5 |
| 500 | Late Afternoon | 4 | 0.5 |
| 400 | Late Afternoon | 3 | 0.375 |
Worked solution and explanation
What this really is
Strip the reliability story and this is a rate comparison across slots of unequal width, finished with a tie-preserving top-N. Morning spans 12 clock hours, Early Afternoon 4 and Late Afternoon 8, so raw counts flatter the long slot. Status 401 piles up 7 Morning failures, yet status 500 fits 4 into the 4-hour Early Afternoon and fails almost twice as often per hour. Dividing by the slot length fixes the comparison, and the division is where the trap sits. COUNT(*) / 12 is integer arithmetic, so 7 / 12 truncates to 0 and every pair except 500 Early Afternoon drops to a zero rate. The 4 highest rates become just 1 and 0, which lets all 20 pairs through.
The tie rule finishes it. 5 pairs share the rate 0.5 just below the top 2, so DENSE_RANK earns its place: it numbers the rate values themselves, and rate_rank <= 4 reaches down to 400 Late Afternoon at 0.375. LIMIT 4 keeps 4 of the 8 qualifying rows in whatever order the engine produced. RANK numbers all 5 tied pairs 3, jumps to 8 and loses that same pair.
The 3 decisions
Step 1: Filter on err_msg before grouping
Only 55 of the 670 calls carry an error message. Put WHERE err_msg IS NOT NULL ahead of the grouping, and only real failures reach the slots. Drop it and the successful traffic, led by status 200, floods every slot until the rates describe total volume.
Step 2: Bucket the hour with an inclusive middle edge
strftime('%H', call_time) returns a 2-character, zero-padded string, so comparing it with '12' and '15' as text orders correctly: '09' sorts before '12'. Reading the hour as an integer works equally well. The middle slot is inclusive at 15, so the second branch reads <= '15'. Write < '15' and hour 15 slides into 'Late Afternoon', moving a whole hour of failures into the wrong denominator.
Step 3: Divide in real arithmetic
Cast the count before dividing: CAST(COUNT(*) AS REAL) / 12 keeps 0.583 where integer division keeps 0. A short CASE on time_segment supplies the slot length (12, 4, 8) in the aggregate stage. That gives the rate a name once, and the ranking stage orders by failures_per_hour directly.
The solution
WITH failures AS (
SELECT
status,
CASE
WHEN strftime('%H', call_time) < '12' THEN 'Morning'
WHEN strftime('%H', call_time) <= '15' THEN 'Early Afternoon'
ELSE 'Late Afternoon'
END AS time_segment
FROM api_calls
WHERE err_msg IS NOT NULL
),
segmented AS (
SELECT
status,
time_segment,
COUNT(*) AS call_count,
CAST(COUNT(*) AS REAL) / CASE time_segment
WHEN 'Morning' THEN 12
WHEN 'Early Afternoon' THEN 4
ELSE 8
END AS failures_per_hour
FROM failures
GROUP BY status, time_segment
),
ranked AS (
SELECT
status,
time_segment,
call_count,
failures_per_hour,
DENSE_RANK() OVER (ORDER BY failures_per_hour DESC) AS rate_rank
FROM segmented
)
SELECT status, time_segment, call_count, failures_per_hour
FROM ranked
WHERE rate_rank <= 4- RANK
RANK()numbers the rates 1, 2, 3, 3, 3, 3, 3, 8. The filter<= 4stops at the tie and drops 400 Late Afternoon at 0.375.- `DENSE_RANK`
DENSE_RANK()numbers the rates 1, 2, 3, 3, 3, 3, 3, 4. The filter<= 4keeps the full tie and the 0.375 pair: 8 rows.
Common follow-up questions
- If you replaced `DENSE_RANK` with `RANK`, which pair would drop out on this data? (400 Late Afternoon at 0.375: the 5-way tie at 0.5 pushes its rank to 8.)
- What happens to the result if the `CAST` is removed? (Integer division truncates every rate below 1 to 0, and all 20 pairs pass the filter.)
- Which slot does hour 15 fall into, and what changes if the branch reads `< '15'`? (Early Afternoon. With `< '15'` its failures move to the 8-hour Late Afternoon denominator.)
- Rewrite this so each HTTP status gets its own busiest slot. (Use `DENSE_RANK() OVER (PARTITION BY status ORDER BY failures_per_hour DESC)` and keep rank 1.)
- What if the rate should divide by the hours that actually logged a failure? (Count unique hours per pair, for example `COUNT(DISTINCT strftime('%H', call_time))`, and divide by that.)