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

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 8 rows
statustime_segmentcall_countfailures_per_hour
500Early Afternoon41
401Morning70.5833333333333334
403Early Afternoon20.5
500Late Afternoon40.5
400Late Afternoon30.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

Filter, bucket, divide by slot length, keep the 4 highest rates
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 <= 4 stops 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 <= 4 keeps the full tie and the 0.375 pair: 8 rows.

Common follow-up questions