Longest Viewing Streak Allowing at Most K Skipped Videos
Last updated: November 29, 2026
Quick Overview
A session is a binary array where 1 means the user watched a video to completion and 0 means they swiped away early. Return the length of the longest contiguous stretch that contains at most k skips.
TikTok
November 29, 20260
0
0 solved
A session is a binary array where 1 means the user watched a video to completion and 0 means they swiped away early. Return the length of the longest contiguous stretch that contains at most k skips.
For the For You feed, TikTok teams care about how long a user stays engaged despite occasional skips, so this sliding window question shows up in coding screens. The interviewer is checking that you can maintain a variable window with an invariant and move both pointers in amortized linear time.
What the Interviewer Expects
- Defines the window invariant clearly: number of zeros in window is at most k
- Expands the right pointer every step and shrinks the left only when the invariant breaks
- Updates the best length after restoring the invariant
- Handles k = 0 and k larger than the number of zeros
- Explains the amortized O(n) bound
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
- How would you also return the start and end index of the best streak?
- What if skips have different weights (a fast skip costs 2, a slow skip costs 1)?
- How would you answer this for many different k values on the same session efficiently?
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 of 0/1 values, integer
k >= 0. - Output: maximum length of a contiguous subarray with at most k zeros.
- n up to 10^5 or more, need linear time....
High-Level Design
Variable-size sliding window. The invariant is zeros_in_window <= k. I move right forward one step at a time. When adding a 0 makes zeros exceed k, I move left forward until a 0 leaves the windo...