Latency Gap to 10th Fastest

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
Civis Analytics

Problem

During an SRE review, the VP of Infrastructure asked how server 'web-prod-01' compares to the 10th-fastest server by average response time. Calculate the absolute difference in their average response times. Return a single latency gap value.

One server. Compared to the 10th fastest.

Tables

server_logs

Columns: log_id INTEGER, server_name TEXT, log_level TEXT, message TEXT, response_time_ms INTEGER, log_timestamp TEXT

server_logs: 10 of 200 rows
log_idserver_namelog_levelmessageresponse_time_mslog_timestamp
137web-prod-02WARNConnection timeout after 30s232026-02-02 01:07:13
174api-prod-01ERRORDatabase query slow: 2.5sNULL2026-03-03 02:14:26
211api-prod-02DEBUGCache miss for key user_123492026-04-04 03:21:39
248db-primaryCRITICALMemory usage at 85%02026-05-05 04:28:52
285db-replica-01INFOSSL certificate expires in 7 daysNULL2026-06-06 05:35:05
322cache-01WARNRate limit exceeded for IP 192.168.1.x882026-07-07 06:42:18
359worker-01ERRORDisk space low: 10% remaining1012026-08-08 07:49:31
396cdn-edge-usDEBUGNew deployment v2.3.1 startedNULL2026-09-09 08:56:44
433cdn-edge-euCRITICALHealth check passed1272026-01-10 09:03:57
470monitor-01INFOFailed to connect to upstream1402026-02-11 10:10:10

Expected output

First 1 of 1 rows
latency_gap
39

Worked solution and explanation

What this really is

This is a two-stage problem wearing a single-number costume: collapse the log into one average per server_name, put those averages in speed order, then pull one value by name and one by position and subtract. Everyone gets the GROUP BY. What separates candidates is treating the 10th position as a contract: a bare ORDER BY avg_rt LIMIT 1 OFFSET 9 returns whichever server the engine emits first when two averages tie, and the gap quietly moves between runs. The second trap is the third of the rows with no response_time_ms: AVG already skips them, and hand-rolling the division or coalescing them to zero drags every server's average down.

Step 1: Average each server once

In server_avg, AVG(response_time_ms) per server_name turns 200 log rows into 11 server rows. AVG divides by the non-NULL count, so a request whose timing was never captured neither adds latency nor counts as a request. The single genuine 0 ms reading on 'db-primary' is real data and stays in.

Step 2: Number the servers by speed

In ranked, ROW_NUMBER() OVER (ORDER BY avg_rt ASC, server_name ASC) gives the fastest server rnk = 1. ROW_NUMBER hands out one number per row, and server_name settles any tie alphabetically, so rnk = 10 always names exactly one server. With 11 servers on the seed, the 10th-fastest is the second-slowest: 'cdn-edge-us' at 757.5 ms.

Step 3: Lift two scalars and take the gap

One scalar subquery reads 'web-prod-01' from server_avg by name (796.5 ms), and the other reads rnk = 10 from ranked. ABS of the difference gives 39.0, and it stays positive whichever server turns out slower.

The solution

Average per server, number by speed, then the absolute gap
WITH server_avg AS (
  SELECT server_name,
         AVG(response_time_ms) AS avg_rt
  FROM server_logs
  GROUP BY server_name
),
ranked AS (
  SELECT server_name,
         avg_rt,
         ROW_NUMBER() OVER (ORDER BY avg_rt ASC, server_name ASC) AS rnk
  FROM server_avg
)
SELECT ABS(
         (SELECT avg_rt FROM server_avg WHERE server_name = 'web-prod-01')
         - (SELECT avg_rt FROM ranked WHERE rnk = 10)
       ) AS latency_gap;
Offset pick
ORDER BY avg_rt LIMIT 1 OFFSET 9 with no second key. It happens to be right on this seed, where no two averages tie, but a tie around position 10 lets the engine return either server, and the gap changes from run to run.
Numbered pick
ROW_NUMBER() OVER (ORDER BY avg_rt, server_name) filtered to rnk = 10. It yields one row, the same row on every engine, and the scalar subquery can never see two values.

Common follow-up questions