Coworking Busy Streaks
A coworking space logs one row per opening day with the number of member check-ins. Days the space was closed have no row, so day_no can skip numbers.
A day is busy when checkins is 120 or more. Return every busy day that belongs to a run of at least three busy days with consecutive day_no values (for example 7, 8, 9). A missing day_no breaks a run, just like a quiet day does.
Columns: day_no, open_date, checkins, ordered by day_no ascending. Return no rows if there is no such run.
Tables
daily_checkins — One row per day the space was open.
| Column | Type | Description |
|---|---|---|
day_no (key) | INTEGER | Sequential day number; numbers of closed days are missing |
open_date | TEXT | Calendar date (YYYY-MM-DD) |
checkins | INTEGER | Member check-ins that day (0 or more) |
Example
daily_checkins
| day_no | open_date | checkins |
|---|---|---|
| 1 | 2026-03-02 | 95 |
| 2 | 2026-03-03 | 130 |
| 3 | 2026-03-04 | 120 |
| 4 | 2026-03-05 | 141 |
| 5 | 2026-03-06 | 88 |
| 6 | 2026-03-07 | 125 |
| 7 | 2026-03-08 | 160 |
| 8 | 2026-03-09 | 150 |
| 9 | 2026-03-10 | 119 |
| 10 | 2026-03-11 | 133 |
| 11 | 2026-03-12 | 121 |
| 13 | 2026-03-14 | 140 |
Expected output
| day_no | open_date | checkins |
|---|---|---|
| 2 | 2026-03-03 | 130 |
| 3 | 2026-03-04 | 120 |
| 4 | 2026-03-05 | 141 |
| 6 | 2026-03-07 | 125 |
| 7 | 2026-03-08 | 160 |
| 8 | 2026-03-09 | 150 |
Return the rows in exactly the order described. Your query also runs against hidden datasets with tricky edge cases.
SQL Path · Product Analytics · problem 65 of 70 · SQLite
The idea
This is the gaps-and-islands pattern. An island is a maximal stretch of busy days with consecutive numbers.
Filter to busy days first, then number them with ROW_NUMBER() OVER (ORDER BY day_no). Inside an island both day_no and the row number go up by 1 per row, so day_no - row_number is constant. Wherever a quiet day or a closed day is skipped, day_no jumps by more than 1 while the row number still goes up by 1, so the difference changes and a new island starts.
With that key, COUNT(*) OVER (PARTITION BY island) gives each row the length of its island, and keeping lengths of at least 3 returns every day of every long run, including its first and last days.
Filtering before numbering is essential: if you number all rows first, quiet days get row numbers and the key no longer separates runs. The alternative uses self-joins: a day qualifies if it is the first, middle or last day of a busy triple of consecutive numbers, with DISTINCT because a day can belong to several triples.
Solution
WITH busy AS (
SELECT
day_no,
open_date,
checkins,
day_no - ROW_NUMBER() OVER (ORDER BY day_no) AS island
FROM daily_checkins
WHERE checkins >= 120
),
sized AS (
SELECT *, COUNT(*) OVER (PARTITION BY island) AS run_length
FROM busy
)
SELECT day_no, open_date, checkins
FROM sized
WHERE run_length >= 3
ORDER BY day_no;
Another way
-- a day qualifies if it is the first, middle or last day of some busy triple
SELECT DISTINCT a.day_no, a.open_date, a.checkins
FROM daily_checkins AS a
JOIN daily_checkins AS b ON b.checkins >= 120
JOIN daily_checkins AS c ON c.checkins >= 120
WHERE a.checkins >= 120
AND (
(b.day_no = a.day_no + 1 AND c.day_no = a.day_no + 2)
OR (b.day_no = a.day_no - 1 AND c.day_no = a.day_no + 1)
OR (b.day_no = a.day_no - 2 AND c.day_no = a.day_no - 1)
)
ORDER BY a.day_no;
Why common answers fail
LAG/LEAD look at the neighbouring rows, not the neighbouring day numbers, so a run that jumps over a closed day is wrongly accepted.
WITH w AS (
SELECT day_no, open_date, checkins,
LAG(checkins, 2) OVER (ORDER BY day_no) AS p2, LAG(checkins) OVER (ORDER BY day_no) AS p1,
LEAD(checkins) OVER (ORDER BY day_no) AS n1, LEAD(checkins, 2) OVER (ORDER BY day_no) AS n2
FROM daily_checkins
)
SELECT day_no, open_date, checkins FROM w
WHERE checkins >= 120
AND ((p2 >= 120 AND p1 >= 120) OR (p1 >= 120 AND n1 >= 120) OR (n1 >= 120 AND n2 >= 120))
ORDER BY day_no;
Uses > 120, so days with exactly 120 check-ins are not treated as busy.
WITH busy AS (
SELECT day_no, open_date, checkins, day_no - ROW_NUMBER() OVER (ORDER BY day_no) AS island
FROM daily_checkins WHERE checkins > 120
)
SELECT day_no, open_date, checkins
FROM (SELECT *, COUNT(*) OVER (PARTITION BY island) AS n FROM busy)
WHERE n >= 3 ORDER BY day_no;
Only returns the middle day of each busy triple, so the first and last days of every run are missing.
SELECT b.day_no, b.open_date, b.checkins
FROM daily_checkins a
JOIN daily_checkins b ON b.day_no = a.day_no + 1
JOIN daily_checkins c ON c.day_no = b.day_no + 1
WHERE a.checkins >= 120 AND b.checkins >= 120 AND c.checkins >= 120
ORDER BY b.day_no;
Numbers all rows before filtering, so the island key only detects missing day numbers and quiet days no longer break a run.
WITH numbered AS (
SELECT day_no, open_date, checkins, day_no - ROW_NUMBER() OVER (ORDER BY day_no) AS island
FROM daily_checkins
)
SELECT day_no, open_date, checkins
FROM (SELECT *, COUNT(*) OVER (PARTITION BY island) AS n FROM numbered WHERE checkins >= 120)
WHERE n >= 3 ORDER BY day_no;
What the interviewer is checking
The day_no minus ROW_NUMBER trick is the canonical gaps-and-islands technique. Interviewers also watch for whether you notice that adjacent rows are not necessarily adjacent days.
Mistakes to avoid
- Using LAG/LEAD by position when the id sequence has gaps.
- Returning only the middle day of each triple.
- Numbering rows before filtering to busy days.
- Off-by-one on the threshold (> vs >=).
Follow-ups to expect
- Return one row per streak with its start, end and length (GROUP BY island).
- Find the longest streak ever (ORDER BY run_length DESC LIMIT 1, decide on ties).
- Use real dates instead of day numbers (julianday(open_date) - ROW_NUMBER()).
In real work
Streak detection drives login-streak badges, "N days above capacity" alerts and outage windows built from consecutive failing health checks.