Problems / Consecutive Login Streaks / Editorial
This is the classic islands and gaps problem. The goal is to identify maximal sequences of consecutive dates.
For consecutive dates, subtracting a sequential row number produces the same value:
| login_date | ROW_NUMBER | date - rn |
|-----------|-----------|----------|
| Jan 1 | 1 | Dec 31 |
| Jan 2 | 2 | Dec 31 |
| Jan 3 | 3 | Dec 31 |
| Jan 6 | 4 | Jan 2 |
| Jan 7 | 5 | Jan 2 |
Grouping by (user_id, date - row_number) identifies each island.
(user_id, date - row_number)
Solve Consecutive Login Streaks yourself →