← TikTok Interview Insights

TikTok·Data Scientist·Technical Phone Screen·Intermediate

Intermediate
Apr 2026

Summary

TikTok data scientist interview with a dynamic programming problem disguised as a feed recommendation scenario. The constraint made it feel applied but it's really just a subsequence optimization with a pairwise condition. Follow-up added repeats with a cap which threw me a bit.

Questions Asked (2)

Q1

Given a list of videos with durations and a user attention span limit A, select a subsequence of videos (preserving order) that maximizes total watch time, subject to the constraint that any two consecutively watched videos have durations summing to at most A. What algorithm do you use and what's the complexity?

Algorithms & Data Structures
Author's notes

Took me a minute to see this was a DP on subsequences with a pairwise transition constraint.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem constraints and define the objective function. Then propose a dynamic programming solution where dp[i] represents the maximum total watch time ending at video i, and transition from previous videos j where duration[j] + duration[i] <= A. Analyze time and space complexity, and discuss potential optimizations.

Pro tip: Mention that this is similar to the Longest Increasing Subsequence but with a sum constraint, and that you can optimize the DP using a segment tree or Fenwick tree to achieve O(n log n) time by querying the maximum dp value among videos with duration <= A - duration[i].

1. Clarify the problem

Restate the problem to ensure understanding: we need to select a subsequence of videos preserving order, maximizing total duration, with the constraint that any two consecutive selected videos have durations summing to at most A.

2. Define DP state and recurrence

Let dp[i] be the maximum total watch time for a valid subsequence ending with video i. Then dp[i] = duration[i] + max(dp[j]) for all j < i such that duration[j] + duration[i] <= A, or just duration[i] if no such j exists.

3. Compute the DP efficiently

Naively, this takes O(n^2) time. To optimize, maintain a data structure (e.g., segment tree or Fenwick tree) keyed by video duration to query the maximum dp value among videos with duration <= A - duration[i] in O(log n) time.

4. Analyze complexity

The optimized DP runs in O(n log n) time and O(n) space. The naive DP runs in O(n^2) time and O(n) space. Discuss trade-offs and when each is appropriate.

5. Consider edge cases and extensions

Handle cases where no valid subsequence exists (return 0), or where A is very large (then it's just the sum of all durations). Also mention that the problem can be extended to weighted videos or other constraints.

Key Points to Mention

  • Dynamic programming state definition and recurrence relation
  • Constraint handling: ensuring consecutive videos satisfy duration sum <= A
  • Time complexity: O(n^2) naive vs O(n log n) optimized with segment tree/Fenwick tree
  • Space complexity: O(n) for DP array and data structure
  • Comparison to similar problems like Longest Increasing Subsequence (LIS) and Weighted Interval Scheduling
  • Edge cases: empty list, no valid pairs, A smaller than any two durations

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

Follow-up: if the user can rewatch videos, but is limited to watching at most K videos total (including repeats), how do you modify the approach to maximize total watch time under the same consecutive-sum constraint?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This one actually stumped me more than I expected.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Recognize that allowing repeats with a total count constraint K transforms the problem into a resource allocation over a sequence, where each video can be watched multiple times but the consecutive-sum constraint still applies. Propose a dynamic programming solution that tracks the current position, remaining watches, and the last watched video's value to enforce the constraint, or alternatively model it as a shortest path problem with state (index, count, last_value).

Pro tip: Emphasize that the DP state must include the last watched value to enforce the consecutive-sum constraint, and discuss how to optimize space/time by noting that only the last value matters, not the entire history. Also, mention that if K is large, greedy approaches may fail, so DP is necessary.

1. Clarify the problem and constraints

Restate the problem: given a sequence of video watch times, you can watch at most K videos total (with repeats allowed), and you cannot watch two videos consecutively if their sum exceeds a threshold? Or the consecutive-sum constraint means the sum of any two consecutive watched videos must be ≤ some limit? Clarify the exact constraint and whether repeats mean watching the same video multiple times in a row or at different times.

2. Define the DP state

Define dp[i][k][last] as the maximum total watch time achievable considering videos up to index i (or from i onward), with k watches remaining, and the last watched video's value being 'last' (to enforce the consecutive-sum constraint). Alternatively, if repeats are allowed, the state might be dp[k][last] where k is remaining watches and last is the last watched value, but we also need to consider the position in the sequence if order matters.

3. Formulate transitions

For each state, consider watching the next video j (which could be any video, including repeats) such that the sum of last and value[j] satisfies the constraint. Update dp[k][j] = max(dp[k][j], dp[k-1][last] + value[j]) if constraint holds. If repeats are allowed, j can be any index, not just the next in sequence.

4. Handle base cases and initialization

Initialize dp[0][last] = 0 for all last (or a sentinel value indicating no last video). For the first watch, there is no consecutive constraint, so we can pick any video. Iterate over k from 1 to K, and for each k, iterate over possible last values and next videos.

5. Optimize and analyze complexity

Discuss time and space complexity. If there are N videos and K watches, naive DP is O(K * N^2) if we consider all pairs. Optimize by noting that for a given last value, we only need the best previous state that satisfies the constraint. Use prefix maxima or segment trees to reduce to O(K * N log N) or O(K * N). Also, consider if K is small, DP is fine; if K is large, maybe there's a greedy or flow-based approach.

Key Points to Mention

  • Dynamic programming with state including remaining watches and last watched value to enforce consecutive-sum constraint.
  • Repeats allowed means transitions can go to any video, not just the next in sequence, increasing branching factor.
  • Consecutive-sum constraint: only the last watched value matters, not the entire history, enabling state compression.
  • Complexity analysis: O(K * N^2) naive, can optimize to O(K * N) with careful transitions.
  • Edge cases: K=0, no valid sequence, constraint impossible to satisfy.
  • Alternative approaches: graph shortest path with resource constraints, or integer linear programming for small N.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.