Average API Latency by Year

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
ESPN

Problem

How has each endpoint's average latency in api_calls moved from one calendar year to the next? List every endpoint's unrounded average for every year, earliest year first and endpoints alphabetical within a year.

Latency year over year. Is it getting better?

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 30 rows
call_yearendpointavg_latency
2023/api/v1/auth/login225.9
2023/api/v1/auth/logout243.2
2024/api/v1/auth/login641.1
2025/api/v1/auth/login1056.3
2026 /api/v2/products81.9

Worked solution and explanation

This is a re-bucketing problem dressed up as a latency review. api_calls has no year column: the only clock is call_time, stamped to the second, so the grain the question asks for (one row per year and endpoint) has to be manufactured before anything is averaged. Everyone writes AVG(latency) correctly. What separates candidates is grouping on the derived year instead of the raw timestamp, or dropping the year entirely. Group on call_time and every 'average' is one call's latency. Group on endpoint alone and '/api/v1/auth/login' collapses into one blended number, hiding its climb from 225.9 in 2023 to 1056.3 in 2025.

Build it from the grain down

Step 1: Say the output grain before writing SQL

One row per calendar year per endpoint. Saying that sentence out loud tells you the year is a key you must compute, because nothing in api_calls stores it. Candidates who skip this step reach for whatever date column is lying around and group on it as-is.

Step 2: Derive the year from call_time

strftime('%Y', call_time) turns '2026-02-02 01:07:00' into '2026'. Alias it call_year and group on that alias together with endpoint. The timestamp itself never appears in the GROUP BY; only its year does.

Step 3: Average, then sort by the two keys

AVG(latency) per group gives avg_latency. Sorting by call_year then endpoint lays each year out as a block, so reading one route down the years is how the trend shows up.

Average latency per year and endpoint
SELECT strftime('%Y', call_time) AS call_year, endpoint, AVG(latency) AS avg_latency FROM api_calls GROUP BY call_year, endpoint ORDER BY call_year, endpoint
Blended per endpoint
GROUP BY endpoint returns one row per route. '/api/v1/auth/login' averages its 2023, 2024 and 2025 calls into a single figure, so 'is latency trending up?' has no answer in the result.
Bucketed per year
GROUP BY call_year, endpoint gives that same route 225.9, 641.1 and 1056.3 across those three years. The climb is the whole point of the review, and it only exists at this grain.

Common follow-up questions