Longest Stretch of Unique Songs in a YouTube Music Play History
Last updated: November 29, 2026
Quick Overview
Given the sequence of song IDs a listener played, return the length of the longest contiguous stretch in which no song repeats.
0
0
0 solved
Given the sequence of song IDs a listener played, return the length of the longest contiguous stretch in which no song repeats.
A classic variable-size sliding window in the style of a Google phone screen, framed around measuring listening variety in YouTube Music. The interviewer checks that the left edge jumps forward using last-seen positions and never moves backward.
What the Interviewer Expects
- Uses a variable window with a last-seen index map
- Moves the left edge to one past the previous occurrence
- Guards against moving the left edge backward
- Updates the best length on every step
- States O(n) time and O(min(n, distinct)) space
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
- Return the actual start and end positions of the best stretch.
- What if one repeat is allowed inside the stretch?
- How would you answer this for every user in a daily batch job over billions of plays?
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: list
playsof hashable song IDs. - Output: maximum length of a contiguous sublist with all distinct IDs.
- Empty history returns 0.
- Target O(n)....
High-Level Design
Keep a window [left, right] that always has unique songs, and a map last from song to its most recent index. When we add plays[right] and it was seen at index p >= left, the window now contain...