Maximize watch time under adjacency constraint
Company: Bytedance
Role: Data Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This Coding & Algorithms question for a Data Engineer role evaluates dynamic programming and sequence-optimization skills by requiring selection of a subsequence under a pairwise adjacency constraint on video durations, at a moderate-to-advanced algorithmic abstraction level.
Part 1: Maximize Subsequence Watch Time Under Adjacent Duration Limit
Constraints
- 0 <= len(duration) <= 200000
- 0 <= duration[i] <= 10^9
- 0 <= A <= 10^9
- The answer fits in a signed 64-bit integer.
Examples
Input: ([], 10)
Expected Output: 0
Explanation: There are no videos, so the best total watch time is 0.
Input: ([12], 5)
Expected Output: 12
Explanation: A single video has no adjacent selected pair, so it can be watched even though its duration exceeds A.
Hints
- Let dp[i] be the maximum total watch time of a valid subsequence that must end at video i.
- For video i, you need the best dp[j] among earlier videos j where duration[j] <= A - duration[i]. Maintain these best values by duration and query a prefix maximum.
Part 2: Maximize Exact-Length Watch Session With Rewatching
Constraints
- 0 <= len(duration) <= 2000
- 0 <= m <= 2000
- 0 <= duration[i] <= 10^9
- 0 <= A <= 10^9
- The answer fits in a signed 64-bit integer.
Examples
Input: ([], 10, 0)
Expected Output: 0
Explanation: Exactly zero selections means the empty session, which is valid.
Input: ([12, 3], 5, 1)
Expected Output: 12
Explanation: With only one selection, there is no adjacency constraint, so choose the longest video.
Hints
- Use dynamic programming over the session length: dp[t][d] can represent the best total for a length-t session ending with duration d.
- For each next duration d, the previous duration must be at most A - d. Sorting distinct durations lets you use prefix maximums to speed up each transition round.