Sign in to run and submit your work
Reading is open to everyone. Running code and saving drafts need an account so your work is yours and comes back on your next visit.
or
CODE WORKSPACE
The engagement team is launching streak badges and needs each user's longest run of consecutive active days. The activity export is one row per session, so it arrives unordered and the same date appears many times, and it spans month ends, a new year and a leap year.
Write longest_streak(active_dates). It takes active_dates, an unordered list of "YYYY-MM-DD" date strings that may repeat, and returns a dict {"length": int, "start": str, "end": str} describing the longest run of consecutive calendar days.
Function to write
longest_streak(active_dates: list[str]) -> dictA dict with the length, start date and end date of the longest run of consecutive calendar days.
How to approach it
A set answers 'was yesterday active?' in O(1) — use that to find where streaks begin, and let real dates, not string digits, decide what yesterday is.
Sample cases
+ 2 held back until you submit
a streak across new year
2025-12-30 to 2026-01-02 is four consecutive days, crossing both a month end and a year end.
Input
Argument 1
[
'2025-12-30',
'2026-01-02',
'2025-12-31',
'2026-01-01',
'2025-12-30',
'2026-01-05'
]Returns
{
'length': 4,
'start': '2025-12-30',
'end': '2026-01-02'
}a leap day joins february to march
2024 is a leap year, so 2024-02-28, 02-29, 03-01 and 03-02 are one four-day streak.
Input
Argument 1
[
'2024-03-01',
'2024-02-28',
'2024-02-29',
'2024-03-02',
'2024-02-10'
]Returns
{
'length': 4,
'start': '2024-02-28',
'end': '2024-03-02'
}two streaks of the same length
Two two-day streaks tie on length; the one that starts earliest, 2025-06-02, wins.
Input
Argument 1
[
'2025-06-10',
'2025-06-11',
'2025-06-03',
'2025-06-02',
'2025-06-20'
]Returns
{
'length': 2,
'start': '2025-06-02',
'end': '2025-06-03'
}no activity at all
An empty list returns length 0 with start and end both None, not an exception.
Input
Argument 1
[] (empty list)
Returns
{
'length': 0,
'start': None,
'end': None
}Constraints
{"length": n, "start": "YYYY-MM-DD", "end": "YYYY-MM-DD"} where start and end are the first and last days of the run, formatted like the input. A single isolated day is a run of length 1 with start equal to end.start.{"length": 0, "start": None, "end": None}.Worked example
Take ["2025-12-30", "2026-01-02", "2025-12-31", "2026-01-01", "2025-12-30", "2026-01-05"]. Parse each into a date and put them in a set, which drops the second 2025-12-30. Now look for days whose previous day is missing: 2025-12-29 is absent, so 2025-12-30 starts a run; 2026-01-04 is absent, so 2026-01-05 starts one. The other days each have a predecessor, so they are never used as a start. From 2025-12-30 step forward a day at a time: 12-31 is there, then 2026-01-01, then 2026-01-02, then 2026-01-03 is not. That run is 4 days; the one from 2026-01-05 is 1. Return length 4, start 2025-12-30, end 2026-01-02.
The trap: sorting the strings and checking whether the day digits go up by one splits this streak in two, because 31 + 1 is not 01 and the year changes too. Only real date arithmetic knows the day after 2025-12-31 is 2026-01-01.
What this tests
A hash set for O(1) membership, walking only from run starts: LeetCode 'Longest Consecutive Sequence' in data clothing. The target is O(n) time and O(n) space — sorting is O(n log n), and walking forward from every day is O(n²) on one long streak. A data engineer meets it for real computing retention, streaks and gaps-and-islands over daily activity. It is not named in the data engineering interview reports we could find; it is here because a streak of active days is the most common place a data engineer meets it.
longest_streak(active_dates: list[str]) -> dictSubmit for review to find out what your query gets right, what it gets wrong, and how it compares with the best working query for this exercise.
This scenario runs a full workspace — editor, canvas and results side by side. It needs a laptop or desktop to be usable. Open this page on a bigger screen to start building.