Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
Operations publish an average delivery time per city, and the cities keep telling them it does not match what couriers see on the road. One four-hour delivery is enough to move a city's average by ten minutes.
Report the median delivery time for each city instead: the middle value once that city's deliveries are sorted by minutes. One row per city, ordered by city.
Result columns · in this order
city | The city. |
deliveries | How many deliveries that city made in total. |
median_minutes | The middle delivery time, to one decimal place. |
How to approach it
Number the rows within each city by minutes, and also count the rows in each city. Then keep only the middle position or positions.
Sample input
| delivery_id | city | courier | minutes | delivered_on |
|---|---|---|---|---|
| 1 | Bengaluru | Asha | 18 | 2026-03-01 |
| 2 | Bengaluru | Asha | 22 | 2026-03-01 |
| 3 | Bengaluru | Rohit | 26 | 2026-03-02 |
| 4 | Bengaluru | Asha | 28 | 2026-03-02 |
| 5 | Bengaluru | Rohit | 30 | 2026-03-03 |
| 6 | Bengaluru | Meera | 30 | 2026-03-03 |
| 7 | Bengaluru | Meera | 34 | 2026-03-04 |
| 8 | Bengaluru | Rohit | 41 | 2026-03-04 |
| 9 | Bengaluru | Asha | 150 | 2026-03-05 |
| 10 | Pune | Kabir | 20 | 2026-03-01 |
| 11 | Pune | Kabir | 24 | 2026-03-01 |
| 12 | Pune | Nita | 26 | 2026-03-02 |
| 13 | Pune | Nita | 30 | 2026-03-02 |
| 14 | Pune | Kabir | 33 | 2026-03-03 |
| 15 | Pune | Nita | 36 | 2026-03-03 |
| 16 | Pune | Kabir | 45 | 2026-03-04 |
| 17 | Pune | Nita | 62 | 2026-03-05 |
| 18 | Pune | Kabir | 240 | 2026-03-05 |
| 19 | Chennai | Vikram | 19 | 2026-03-01 |
| 20 | Chennai | Priya | 23 | 2026-03-01 |
| 21 | Chennai | Vikram | 27 | 2026-03-02 |
| 22 | Chennai | Priya | 30 | 2026-03-03 |
| 23 | Chennai | Vikram | 38 | 2026-03-03 |
| 24 | Chennai | Priya | 52 | 2026-03-04 |
| 25 | Chennai | Vikram | 71 | 2026-03-04 |
| 26 | Chennai | Priya | 96 | 2026-03-05 |
26 rows — scroll inside the table to see them all.
Expected output
| city | deliveries | median_minutes |
|---|---|---|
| Bengaluru | 9 | 30 |
| Chennai | 8 | 34 |
| Pune | 9 | 33 |
3 rows — all rows shown.
Constraints
MEDIAN and no PERCENTILE_CONT. Build it from a window function.deliveries must report how many deliveries the city actually made — not how many rows survived your filter.median_minutes to one decimal place, so the even-count city reads honestly.city.Worked example
Chennai has eight deliveries: 19, 23, 27, 30, 38, 52, 71, 96. There is no single middle row, so the median is the average of the 4th and 5th — (30 + 38) / 2 = 34.0.
Bengaluru has nine: 18, 22, 26, 28, 30, 30, 34, 41, 150. The 5th row is the middle, so the median is 30.0 — and its mean is 42.1, because that 150 has to go somewhere.
The trick that covers both: take the rows at positions (n + 1) / 2 and (n + 2) / 2 using integer division. When n is odd those two expressions name the same row, so averaging is a no-op. When n is even they name the two straddling rows.
What this tests
Whether you can express a median without a median function — a standing interview question, because at least one engine every team uses is missing one. Also whether you noticed the even-count case at all.
Submit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.