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.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

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
Hash Set
Arrays
Amortized Analysis
Streaks
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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 ...


Submit Your Answer
Markdown supported