Largest CDN Response

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

Domain
SQL
Difficulty
hard
Seniority
L4
Asked in
ESPN

Problem

Every row in cdn_logs is one response an edge location served, sized in bytes. Return the full record of the largest response served in 2026, and if two responses served the same number of bytes, keep the one with the smaller log id.

One edge location served something massive.

Tables

cdn_logs

Columns: log_id INTEGER, edge_loc TEXT, req_path TEXT, status INTEGER, bytes INTEGER, cache_hit INTEGER, served_at TEXT

cdn_logs: 10 of 200 rows
log_idedge_locreq_pathstatusbytescache_hitserved_at
537SFO/js/app.min.js200143702026-02-02 01:07:13
574LHR/css/main.css200277402026-03-03 02:14:26
611NRT/images/hero.jpg206411112026-04-04 03:21:39
648SIN/api/v1/data301544802026-05-05 04:28:52
685FRA/fonts/roboto.woff2304NULL02026-06-06 05:35:05
722SYD/video/intro.mp44038122NULL2026-07-07 06:42:18
759GRU/favicon.ico4049459NULL2026-08-08 07:49:31
796IAD/assets/logo.png?v=250010796NULL2026-09-09 08:56:44
833SFO/js/app.min.js 50312133NULL2026-01-10 09:03:57
870LHR/assets/logo.png2001347002026-02-11 10:10:10

Expected output

First 1 of 1 rows
log_idedge_locreq_pathstatusbytescache_hitserved_at
4200SIN/assets/logo.png20013380002026-02-17 04:40:40

Worked solution and explanation

What this problem is really testing

Beneath the CDN framing this is a top-1 selection dressed with a dirty column. The bytes field looks numeric but is stored as text, and some rows carry a trailing 'KB' suffix. Type ORDER BY bytes DESC LIMIT 1 and you sort lexically, not by magnitude: a junk string like '99KB' outranks the genuine maximum '133800', so the edge_loc and req_path you report belong to the wrong response entirely. Two more traps sit on top: the largest response overall was served in a different year and must be scoped out, and the true in-year maximum is shared by more than one response, so a bare LIMIT returns whichever row the engine happens to surface.


Building the query

Step 1: Read from cdn_logs

Everything needed is already in cdn_logs, and the output is the full log record, so no join or subquery is required. Select the seven columns directly.

Step 2: Scope to the year

Restrict served_at to the requested year with a half-open range on the timestamp text: from January 1 of 2026 up to, but not including, January 1 of 2027. Skip this and a bigger response from another year wins.

Step 3: Normalize, then sort by size

Order by the NORMALIZED byte count descending: REPLACE the 'KB' suffix out and CAST to INTEGER so magnitude, not lexical text, drives the sort. The largest response floats to the top.

Step 4: Break the tie, then take one

The maximum is not unique within the year, so append log_id ascending as a tiebreaker and take LIMIT 1. The winning row is now a function of the data alone, identical on every run.


The solution

Deterministic top-1 by normalized byte count
SELECT log_id, edge_loc, req_path, status, bytes, cache_hit, served_at
FROM cdn_logs
WHERE served_at >= '2026-01-01' AND served_at < '2027-01-01'
ORDER BY CAST(REPLACE(bytes, 'KB', '') AS INTEGER) DESC, log_id ASC
LIMIT 1

Common follow-up questions