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.

ColumnTypeDescription
view_id (key)INTEGERUnique id
learner_idINTEGERLearner who viewed the lesson
viewed_atTEXTView time, 'YYYY-MM-DD HH:MM:SS'
lesson_idINTEGERLesson that was opened

Example

lesson_views

view_idlearner_idviewed_atlesson_id
1112026-04-03 19:00:00101
2112026-04-04 08:00:00102
3112026-04-06 20:00:00103
4112026-04-08 07:30:00104
5112026-04-09 22:00:00105
6122026-04-01 10:00:00101
7122026-04-01 18:00:00102
8122026-04-02 10:00:00103
9122026-04-03 10:00:00104
10122026-04-03 11:00:00105
11122026-04-04 10:00:00106
12132026-04-01 09:00:00101

… 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

Reduce the data to one row per learner per active day first.
For each active day, count the learner's active days from that day through six days later. If any such count reaches 5, the learner qualifies.
A window frame can do the counting: ORDER BY julianday(day) RANGE BETWEEN CURRENT ROW AND 6 FOLLOWING. A self-join on a date range works too.

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.

Practice next

Next Post Previous Post
No Comment
Add Comment
comment url