Fastest Regions by Latency

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
L3
Asked in
EY

Problem

The api_calls log holds one row per request with the endpoint it hit and its latency, so an endpoint's speed is the average latency over all of its calls. List every endpoint whose average is among the three lowest averages, ties included, alongside that average.

The fastest regions. Benchmarked.

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 3 of 3 rows
endpointavg_latency
/api/v1/orders 41.2
/api/v2/products81.9
/api/v2/analytics148.0193548387097

Worked solution and explanation

This is a top-N over distinct values dressed up as a performance highlight. The skill is ranking the per-endpoint averages so that 'top 3' means the three lowest average figures, not the first three rows: every endpoint that holds one of those figures belongs in the answer. Anyone can write the GROUP BY endpoint with AVG(latency). What separates candidates is refusing ORDER BY AVG(latency) LIMIT 3, which counts rows instead of figures. On the day two endpoints land on the same third-lowest average, LIMIT 3 keeps an arbitrary one and hides the other, and the highlight silently drops an endpoint that is exactly as fast.

Getting there

Step 1: Average each endpoint over all its calls

Collapse api_calls to one row per endpoint with AVG(latency). The six calls whose latency is NULL drop out of the average on their own, which is what you want: a call that was never timed says nothing about speed.

Step 2: Number the averages, not the rows

DENSE_RANK() OVER (ORDER BY AVG(latency) ASC) runs after the grouping, in the same SELECT, so it sees one row per endpoint. Equal averages share a number and the next figure takes the next number, so rnk <= 3 means the three lowest figures however many endpoints hold them.

Step 3: Filter the rank one level up

A window result cannot be tested in the WHERE of the query that computes it, so wrap the ranked aggregate as ranked and keep rnk <= 3 outside. Project endpoint and avg_latency only; the rank is scaffolding.

Rank endpoints by average latency, keep ties at position 3
SELECT endpoint, avg_latency FROM (SELECT endpoint, AVG(latency) AS avg_latency, DENSE_RANK() OVER (ORDER BY AVG(latency) ASC) AS rnk FROM api_calls GROUP BY endpoint) ranked WHERE rnk <= 3
Cut by rows
ORDER BY AVG(latency) LIMIT 3 returns exactly three rows. With averages of 40, 80, 150 and 150 it keeps one of the two 150 endpoints, and which one depends on the plan.
Cut by figures
DENSE_RANK numbers those averages 1, 2, 3, 3, and rnk <= 3 returns all four. Today's data happens to have no tie in the three lowest, so both return the same three rows; only this one is right on every day.

Common follow-up questions