Identify Longest Consecutive Incrementing Watch-Time Sequence
Company: Netflix
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
##### Scenario
A streaming platform records daily minutes watched per user and wants to identify engagement streaks.
##### Question
Given an unsorted integer array representing daily watch-time deltas, return the length of the longest sequence of consecutive, incrementing integers (e.g., 1,2,3…). Explain the algorithm and analyze complexity.
##### Hints
Think hash-set to achieve O(n) time and avoid re-scanning sequences.
Quick Answer: This question evaluates a candidate's ability to design and analyze efficient algorithms for detecting longest consecutive incrementing sequences in unsorted integer arrays, testing understanding of appropriate data structures and time/space complexity trade-offs.
Given an unsorted integer array deltas representing daily watch-time changes, return the length of the longest sequence of values that are consecutive and strictly increment by 1 (k, k+1, k+2, ...). The sequence is formed from values only; indices and original order do not matter. Duplicates do not extend a sequence. If the array is empty, return 0.
Constraints
- 0 <= len(deltas) <= 200000
- -10^9 <= deltas[i] <= 10^9
- Duplicates may appear and should be treated as a single value for sequence building
- Expected average time: O(n) using hashing
- Auxiliary space: O(n)
Hints
- Insert all numbers into a hash set for O(1) average lookups.
- Only start counting from numbers x where x-1 is not in the set (start of a run).
- From each start, incrementally check x+1, x+2, ... to count the run length.
- Duplicates are naturally handled by the set and do not extend runs.