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
A retention question: which couriers keep showing up day after day, and which only appear in bursts? The measure the team agreed on is the longest unbroken stretch of days each courier delivered.
Return one row per courier with longest_streak in days. Order by longest_streak descending, then by courier.
Result columns · in this order
courier | The courier. |
longest_streak | Length in days of their longest unbroken stretch of delivery days. |
How to approach it
Identify the runs exactly as in the previous problem, then take MAX of the run lengths per courier.
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
| courier | longest_streak |
|---|---|
| Vikram | 4 |
| Kabir | 3 |
| Priya | 3 |
| Rohit | 3 |
| Asha | 2 |
| Meera | 2 |
| Nita | 2 |
7 rows — all rows shown.
Constraints
longest_streak descending, then courier ascending.Worked example
Vikram delivered on 2026-03-01, 02, 03 and 04 — four consecutive days and no gap — so his longest streak is 4, the highest in the dataset.
Asha delivered on 03-01, 03-02 and then 03-05. That is a run of two and a run of one, so her longest streak is 2 even though she worked on three separate days.
The distinction between 'days worked' and 'longest streak' is the whole question. Asha and Nita both worked three days; their streaks differ from Vikram's because of where the gaps fall, and a plain COUNT(DISTINCT delivered_on) cannot see that.
What this tests
Whether you can aggregate over the islands rather than over the rows — the second half of gaps and islands, and the half interviews actually ask for.
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.