Maximize watch time under adjacency constraint
ByteDanceData Engineer · Technical ScreenLast updated
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.
You are given a sequence of videos in a feed.
- Input:
- An integer array
duration[1..n], whereduration[i]is the length of videoiin seconds. - An integer
A(the user’s attention limit). - Task:
- Select a subset of videos in the original order (i.e., choose a subsequence).
- Let the selected videos be
i1 < i2 < ... < ik. The constraint is: - For every adjacent pair in the selected subsequence,
duration[ij] + duration[ij+1] <= A. - Maximize the total watch time:
sum(duration[ij]). - Output:
- Return the maximum achievable total watch time.
Follow-up:
- Suppose the user is allowed to rewatch the same video multiple times, and the session consists of exactly
mselections (so repetitions are allowed, but the number of watched items is capped). How would you modify your approach to compute the maximum total watch time under the same adjacency constraint?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.