Top 10 Slowest Endpoints

An easy SQL interview practice problem on DataDriven. Write the query and run it against a real database, with instant feedback.

Domain
SQL
Difficulty
easy
Seniority
L4
Asked in
ESPN

Problem

List every endpoint whose peak single-request latency in api_calls is among the ten highest peaks of 2026, with that peak and its place on the list, slowest first. The on-call lead reading it wants no endpoint cut for sharing a peak with another, even when that pushes the list past ten rows.

The ten endpoints nobody wants to call.

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
endpointmax_latencyrnk
/api/v1/search1713.71
/api/v2/products1696.42
/api/v1/orders1679.13
/api/v1/users1661.84
/api/v1/auth/logout1644.55

Worked solution and explanation

Ten places, not ten rows

This is a top-N over distinct values dressed up as a latency review. Everyone gets the easy part right: filter call_time to 2026, take MAX(latency) per endpoint. What separates candidates is reading "the ten highest peaks" as ten places rather than ten rows. Write the sort-and-cut-at-ten reflex and you drop whichever endpoint tied at the boundary, which is the endpoint the review was convened to catch.


Build it in three moves

Step 1: Scope the calls to 2026

call_time is stored as text, so strftime('%Y', call_time) = '2026' keeps only this year's calls. Both sides are text, so no implicit cast decides whether a row matches.

Step 2: Reduce each endpoint to its peak

GROUP BY endpoint with MAX(latency) AS max_latency collapses each endpoint to its single worst request. The aggregate keeps one running value per endpoint, so a hundred endpoints cost a hundred slots of memory no matter how many calls there were.

Step 3: Number the places, then keep the first ten

DENSE_RANK() OVER (ORDER BY MAX(latency) DESC) can sit in the same SELECT as the GROUP BY because the window runs after aggregation. Tied peaks get one place and the next peak takes the next number. Wrap it, keep rnk <= 10, and sort by rnk with endpoint breaking ties so the output is stable.


The solution

Year filter, peak per endpoint, ten places
SELECT endpoint, max_latency, rnk FROM (
  SELECT endpoint, MAX(latency) AS max_latency,
         DENSE_RANK() OVER (ORDER BY MAX(latency) DESC) AS rnk
  FROM api_calls
  WHERE strftime('%Y', call_time) = '2026'
  GROUP BY endpoint
) ranked
WHERE rnk <= 10
ORDER BY rnk ASC, endpoint ASC
Cut at ten rows
Sorting the peaks descending and keeping ten rows with LIMIT 10 stops at a row count. Two endpoints tied at the cutoff means one vanishes, and a tie higher up means the tenth distinct peak never appears.
Cut at ten places
DENSE_RANK() hands tied peaks one shared rnk, and rnk <= 10 keeps every endpoint holding one of the ten highest peaks, even if that is twelve rows.

Common follow-up questions