Longest Meditation Streak

A meditation app rewards members for practising on consecutive days. A streak is a run of consecutive calendar days on each of which the member checked in at least once; several check-ins on the same day count as one day, and a single missed day ends the streak.

For every member with at least one check-in, return member_id and longest_streak: the length in days of their longest streak (a lone day is a streak of 1). Streaks run across month and year boundaries normally.

Order by member_id. If there are no check-ins, return no rows.

Tables

check_ins — One row each time a member finishes a meditation session.

ColumnTypeDescription
check_in_id (key)INTEGERUnique id
member_idINTEGERMember who meditated
checked_atTEXTCompletion time, 'YYYY-MM-DD HH:MM:SS'

Example

check_ins

check_in_idmember_idchecked_at
112026-01-29 07:00:00
212026-01-30 07:10:00
312026-01-30 21:00:00
412026-01-31 06:55:00
512026-02-01 07:05:00
612026-02-03 07:00:00
722026-02-10 22:00:00
822026-02-12 22:00:00
922026-02-13 08:00:00
1032026-02-20 12:00:00

Expected output

member_idlongest_streak
14
22
31

Return the rows in exactly the order described. Your query also runs against hidden datasets with tricky edge cases.

SQL Path · Dates & Time · problem 49 of 70 · SQLite

First reduce the data to one row per member per calendar day with SELECT DISTINCT member_id, date(checked_at).
Number each member's days in order with ROW_NUMBER(). Within a run of consecutive days, the date and the row number both go up by 1 each step.
So julianday(day) - row_number is constant inside a run and changes after a gap. Group by it to measure each run, then take the MAX per member.

The idea

This is the gaps and islands pattern. Start by deduplicating to one row per member per day; otherwise two check-ins on the same day would look like two different days.

Then number each member's days in date order. For consecutive days, the day number (julianday(day)) and the row number both increase by exactly 1, so their difference stays the same. After a missed day the date jumps by 2 or more while the row number still goes up by 1, so the difference changes. That difference is an island key: every row in one streak shares it.

Group by member and island key to get each streak's length, then take the maximum per member. julianday gives a continuous day count, so month ends, leap days and New Year need no special handling.

The alternative marks the start of each streak with LAG (a day whose previous practice day is not exactly yesterday). A running SUM of those flags then numbers the streaks, and the rest is the same grouping.

Solution

WITH practice_days AS (
  SELECT DISTINCT member_id, date(checked_at) AS day
  FROM check_ins
),
islands AS (
  SELECT member_id,
         julianday(day)
           - ROW_NUMBER() OVER (PARTITION BY member_id ORDER BY day) AS island_key
  FROM practice_days
),
streaks AS (
  SELECT member_id, COUNT(*) AS streak_days
  FROM islands
  GROUP BY member_id, island_key
)
SELECT member_id,
       MAX(streak_days) AS longest_streak
FROM streaks
GROUP BY member_id
ORDER BY member_id;

Another way

WITH practice_days AS (
  SELECT DISTINCT member_id, date(checked_at) AS day
  FROM check_ins
),
flagged AS (
  SELECT member_id, day,
         CASE WHEN LAG(day) OVER (PARTITION BY member_id ORDER BY day) = date(day, '-1 day')
              THEN 0 ELSE 1 END AS starts_streak
  FROM practice_days
),
numbered AS (
  SELECT member_id,
         SUM(starts_streak) OVER (PARTITION BY member_id ORDER BY day) AS streak_no
  FROM flagged
)
SELECT member_id, MAX(streak_days) AS longest_streak
FROM (
  SELECT member_id, COUNT(*) AS streak_days
  FROM numbered
  GROUP BY member_id, streak_no
)
GROUP BY member_id
ORDER BY member_id;

Why common answers fail

Skips deduplicating to one row per day, so a second check-in on the same day shifts the row numbers and splits or miscounts the streak.

WITH islands AS (
  SELECT member_id,
         julianday(date(checked_at)) - ROW_NUMBER() OVER (PARTITION BY member_id ORDER BY checked_at) AS island_key
  FROM check_ins
)
SELECT member_id, MAX(n) AS longest_streak
FROM (SELECT member_id, COUNT(*) AS n FROM islands GROUP BY member_id, island_key)
GROUP BY member_id
ORDER BY member_id;

Uses the day-of-month number instead of a continuous day count, so a streak that crosses into a new month is split in two.

WITH practice_days AS (
  SELECT DISTINCT member_id, date(checked_at) AS day FROM check_ins
), islands AS (
  SELECT member_id,
         CAST(strftime('%d', day) AS INTEGER) - ROW_NUMBER() OVER (PARTITION BY member_id ORDER BY day) AS island_key
  FROM practice_days
)
SELECT member_id, MAX(n) AS longest_streak
FROM (SELECT member_id, COUNT(*) AS n FROM islands GROUP BY member_id, island_key)
GROUP BY member_id
ORDER BY member_id;

Counts all distinct practice days, ignoring that a missed day ends a streak.

WITH practice_days AS (
  SELECT DISTINCT member_id, date(checked_at) AS day FROM check_ins
)
SELECT member_id, COUNT(*) AS longest_streak
FROM practice_days
GROUP BY member_id
ORDER BY member_id;

What the interviewer is checking

Gaps and islands is a favourite senior-level SQL question; it tests whether you can turn "consecutive" into a grouping key with window functions.

Mistakes to avoid

  • Forgetting to deduplicate multiple events on the same day.
  • Using day-of-month instead of a continuous day number.
  • Counting all active days instead of the longest consecutive run.

Follow-ups to expect

  • Return the start and end date of the longest streak (MIN(day) and MAX(day) per island, then pick the top one per member).
  • Show each member's current streak as of today (keep only the island that contains date('now') or yesterday).
  • Allow one missed day without breaking the streak (use LAG to flag gaps larger than 2 days).

In real work

Habit and learning apps show streak counters, and the same island logic finds outages, consecutive failed logins and uninterrupted subscription periods.

Practice next

Next Post Previous Post
No Comment
Add Comment
comment url