Dsa
Sliding Window
Medium
Limited Overlap Stream
You are processing a stream of user IDs. To prevent bot activity, a valid 'session window' is defined as a contiguous subsegment where no single user ID appears more than M times. Find the length of the longest valid session window.
Input Format:
- The first line contains an integer M (the maximum frequency allowed for any ID).
- The second line contains a space-separated sequence of user IDs (integers).
Output Format:
- A single integer representing the length of the longest valid window.
Example:
Input:
2
1 2 1 3 1 1 2
Output:
5
(Explanation: Window [1, 2, 1, 3, 1] is invalid because ID '1' appears 3 times. Window [2, 1, 3, 1, 1] is invalid. The longest valid window is [1, 2, 1, 3, 2] which is not possible here. Let's check [1, 2, 1, 3] which is length 4. Actually, [2, 1, 3, 1, 2] is length 5 and each ID appears <= 2 times.)
Key concepts
sliding_windowhash_map
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now