Count Watch Sessions Whose Retention Deltas Sum to Target
Last updated: November 29, 2026
Quick Overview
You are given an array of integer retention deltas, one per consecutive video in a user's session (positive when watch time rose, negative when it fell). Count the contiguous stretches whose deltas sum exactly to a target k.
TikTok
November 29, 20260
0
0 solved
You are given an array of integer retention deltas, one per consecutive video in a user's session (positive when watch time rose, negative when it fell). Count the contiguous stretches whose deltas sum exactly to a target k.
TikTok phone screens for feed and recommendation backend teams often dress up prefix-sum problems as engagement analytics. The interviewer wants to see that you recognize negative numbers break the sliding window approach and reach for prefix sums with a hash map.
What the Interviewer Expects
- Explains why a two-pointer window fails when values can be negative
- Uses running prefix sums and a count map of previously seen prefixes
- Seeds the map with prefix 0 to count stretches starting at index 0
- Achieves O(n) time and O(n) space
- Tests with zeros and negative values
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 return the longest such stretch instead of the count?
- What if the deltas arrive as a stream and you must report the running count?
- How does the answer change if you want sums divisible by k rather than equal to k?
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 ints
deltas(can be negative or zero), integerk. - Output: number of contiguous subarrays with sum exactly
k. - Length up to around 10^5, so O(n^2) is too slow....
High-Level Design
A sliding window only works when every value is non-negative, because shrinking the window must always reduce the sum. Retention deltas can be negative, so I use prefix sums.
Let P[i] be the sum of...