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 want to see how couriers actually work: not how many days each one delivered, but which stretches of days they worked without a break, and how long each stretch was.
Return one row per unbroken run of consecutive days per courier, with the first day, the last day, and the number of days in it. Order by courier, then run_start.
Result columns · in this order
courier | The courier. |
run_start | First day of this unbroken run. |
run_end | Last day of this unbroken run. |
days_in_run | How many consecutive days the run covers. |
How to approach it
Deduplicate to one row per courier-day, number them, subtract the number from the date, and group on the result.
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 | run_start | run_end | days_in_run |
|---|---|---|---|
| Asha | 2026-03-01 | 2026-03-02 | 2 |
| Asha | 2026-03-05 | 2026-03-05 | 1 |
| Kabir | 2026-03-01 | 2026-03-01 | 1 |
| Kabir | 2026-03-03 | 2026-03-05 | 3 |
| Meera | 2026-03-03 | 2026-03-04 | 2 |
| Nita | 2026-03-02 | 2026-03-03 | 2 |
| Nita | 2026-03-05 | 2026-03-05 | 1 |
| Priya | 2026-03-01 | 2026-03-01 | 1 |
| Priya | 2026-03-03 | 2026-03-05 | 3 |
| Rohit | 2026-03-02 | 2026-03-04 | 3 |
| Vikram | 2026-03-01 | 2026-03-04 | 4 |
11 rows — scroll inside the table to see them all.
Constraints
courier, then run_start.Worked example
Kabir delivered on 2026-03-01, then 2026-03-03, 2026-03-04 and 2026-03-05. Numbered in order those are rows 1, 2, 3, 4. Subtract the row number as days: 03-01 minus 1 day is 02-28; 03-03 minus 2 is 03-01; 03-04 minus 3 is 03-01; 03-05 minus 4 is 03-01. The three consecutive days collapse onto the same key and the isolated day sits on its own — so Kabir has two runs, one of length 1 and one of length 3. That is the whole trick: within a run, the date and the row number increase in lockstep, so their difference is constant. A gap breaks the lockstep and starts a new constant.
What this tests
Gaps and islands — the pattern behind streaks, sessions and uptime windows — and whether you deduplicated the days before numbering them.
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.