Longest Streak of Consecutive Active Days for a User
Last updated: November 29, 2026
Quick Overview
Given an unsorted list of integers representing day numbers on which a user was active (duplicates possible), return the length of the longest run of consecutive days.
0
0
0 solved
Given an unsorted list of integers representing day numbers on which a user was active (duplicates possible), return the length of the longest run of consecutive days.
A warm-up in the style of a Google phone screen, framed around engagement metrics for a product like Google Fit or Duolingo-style streaks. The interviewer wants to see whether you move past the obvious sort-based solution to a linear-time hash set approach and can explain why it is linear.
What the Interviewer Expects
- States the O(n log n) sort approach first, then improves it
- Uses a hash set and only starts counting at the beginning of a run
- Explains why the total work is O(n) despite the nested loop
- Handles duplicates and empty input without special hacks
- Writes clean code with meaningful names
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- What if the days arrive as a stream and you must report the current longest streak after each event?
- How would you allow one missed day inside a streak (a streak freeze)?
- If the data has billions of users, how would you compute this per user in a batch job?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: a list of integers (day indices), unsorted, possibly with duplicates.
- Output: the length of the longest sequence d, d+1, ..., d+k all present.
- Empty input returns 0. Duplicates do not ext...
High-Level Design
Sorting and scanning works in O(n log n). To get linear time, put every day in a set. A day d is the start of a run only if d - 1 is not in the set. From each start, walk forward while d + 1 is ...