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
Coding & Algorithms
Software Engineer
TikTok
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

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
prefix sums
hash maps
subarray counting
negative numbers
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample Answer
Requirements
  • Input: list of ints deltas (can be negative or zero), integer k.
  • 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...


Submit Your Answer
Markdown supported