Learners on a Weekly Roll
An online course platform sends a badge to learners who study on at least 5 different days within some 7-day period. A 7-day period is any 7 consecutive calendar days (for example Friday through the next Thursday), not just Monday-to-Sunday weeks. A learner is active on a day if they viewed at least one lesson that day; many views on one day still count as one day.
Return a single column learner_id listing each qualifying learner once, ordered by learner_id. If nobody qualifies, return no rows.
Tables
lesson_views — One row each time a learner opens a lesson.
| Column | Type | Description |
|---|---|---|
view_id (key) | INTEGER | Unique id |
learner_id | INTEGER | Learner who viewed the lesson |
viewed_at | TEXT | View time, 'YYYY-MM-DD HH:MM:SS' |
lesson_id | INTEGER | Lesson that was opened |
Example
lesson_views
| view_id | learner_id | viewed_at | lesson_id |
|---|---|---|---|
| 1 | 11 | 2026-04-03 19:00:00 | 101 |
| 2 | 11 | 2026-04-04 08:00:00 | 102 |
| 3 | 11 | 2026-04-06 20:00:00 | 103 |
| 4 | 11 | 2026-04-08 07:30:00 | 104 |
| 5 | 11 | 2026-04-09 22:00:00 | 105 |
| 6 | 12 | 2026-04-01 10:00:00 | 101 |
| 7 | 12 | 2026-04-01 18:00:00 | 102 |
| 8 | 12 | 2026-04-02 10:00:00 | 103 |
| 9 | 12 | 2026-04-03 10:00:00 | 104 |
| 10 | 12 | 2026-04-03 11:00:00 | 105 |
| 11 | 12 | 2026-04-04 10:00:00 | 106 |
| 12 | 13 | 2026-04-01 09:00:00 | 101 |
… 3 more rows
Expected output
| learner_id |
|---|
| 11 |
Return the rows in exactly the order described. Your query also runs against hidden datasets with tricky edge cases.
SQL Path · Dates & Time · problem 47 of 70 · SQLite
The idea
Deduplicate first: one row per learner per calendar day. After that, "5 active days within 7 consecutive days" becomes "5 rows within a 7-day range".
It is enough to check windows that start on an active day: if some 7-day period contains 5 active days, then the period starting on the first of those days also contains them. So for every active day, count that learner's active days from the same day through 6 days later and look for a count of at least 5.
SQLite can count this with a value-based frame. Ordering by julianday(day) makes the order key a number, and RANGE BETWEEN CURRENT ROW AND 6 FOLLOWING includes rows whose day number is at most 6 greater, which is exactly 7 calendar days. Because it is a range of values and not a number of rows, missing days are handled correctly.
The alternative self-joins the active days on b.day BETWEEN a.day AND date(a.day, '+6 days') and keeps starting days whose group has at least 5 rows.
Solution
WITH active_days AS (
SELECT DISTINCT learner_id, date(viewed_at) AS day
FROM lesson_views
),
windows AS (
SELECT learner_id,
COUNT(*) OVER (
PARTITION BY learner_id
ORDER BY julianday(day)
RANGE BETWEEN CURRENT ROW AND 6 FOLLOWING
) AS days_in_window
FROM active_days
)
SELECT DISTINCT learner_id
FROM windows
WHERE days_in_window >= 5
ORDER BY learner_id;
Another way
WITH active_days AS (
SELECT DISTINCT learner_id, date(viewed_at) AS day
FROM lesson_views
)
SELECT DISTINCT a.learner_id
FROM active_days AS a
JOIN active_days AS b
ON b.learner_id = a.learner_id
AND b.day BETWEEN a.day AND date(a.day, '+6 days')
GROUP BY a.learner_id, a.day
HAVING COUNT(*) >= 5
ORDER BY a.learner_id;
Why common answers fail
Counts lesson views rather than distinct days, so several views on the same day inflate the count.
WITH windows AS (
SELECT learner_id,
COUNT(*) OVER (PARTITION BY learner_id ORDER BY julianday(date(viewed_at))
RANGE BETWEEN CURRENT ROW AND 6 FOLLOWING) AS days_in_window
FROM lesson_views
)
SELECT DISTINCT learner_id FROM windows WHERE days_in_window >= 5 ORDER BY learner_id;
Off by one: allowing 7 days between the first and fifth active day spans 8 calendar days, not 7.
WITH active_days AS (
SELECT DISTINCT learner_id, date(viewed_at) AS day FROM lesson_views
), nexts AS (
SELECT learner_id, day, LEAD(day, 4) OVER (PARTITION BY learner_id ORDER BY day) AS fifth_day
FROM active_days
)
SELECT DISTINCT learner_id FROM nexts
WHERE julianday(fifth_day) - julianday(day) <= 7
ORDER BY learner_id;
Uses fixed calendar weeks, so five active days that straddle a week boundary are missed.
WITH active_days AS (
SELECT DISTINCT learner_id, date(viewed_at) AS day FROM lesson_views
)
SELECT DISTINCT learner_id
FROM active_days
GROUP BY learner_id, strftime('%Y-%W', day)
HAVING COUNT(*) >= 5
ORDER BY learner_id;
What the interviewer is checking
Tests sliding-window reasoning: deduplicating events into days and choosing a RANGE frame (or a range self-join) instead of fixed calendar buckets.
Mistakes to avoid
- Counting events instead of distinct days.
- Off-by-one window edges (7 days apart is an 8-day span).
- Using fixed calendar weeks instead of a sliding window.
- Using ROWS instead of RANGE, which counts rows regardless of how far apart they are.
Follow-ups to expect
- Return the date each learner first qualified (for each starting day take LEAD(day, 4) as the 5th active day, keep those within 6 days, then MIN per learner).
- Make it 10 active days in any 30-day window (change the frame to 29 FOLLOWING and the threshold).
- Count qualifying learners per month (group the qualifying starting days by strftime('%Y-%m')).
In real work
Power-user definitions such as "5 of the last 7 days", fraud rules ("5 failed payments in 7 days") and engagement badges all use sliding-window counts.