PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

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.

  • hard
  • Bytedance
  • Coding & Algorithms
  • Data Engineer

Maximize watch time under adjacency constraint

Company: Bytedance

Role: Data Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

You are given a sequence of videos in a feed. - Input: - An integer array `duration[1..n]`, where `duration[i]` is the length of video `i` in 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 `m` selections (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?

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

You are given a feed of videos in a fixed order. The length of video i is duration[i]. Choose any subsequence of videos, preserving the original order. For every two adjacent videos in your chosen subsequence, the sum of their durations must be at most A. Return the maximum possible total watch time. A single selected video is always valid because it has no adjacent selected pair. Selecting no videos is allowed and has total watch time 0.

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.

Input: ([4, 2, 6, 3, 5], 8)

Expected Output: 14

Explanation: One optimal subsequence is [4, 2, 3, 5]. Adjacent sums are 6, 5, and 8, and the total is 14.

Input: ([8, 1, 7, 2, 6], 8)

Expected Output: 9

Explanation: One optimal subsequence is [1, 2, 6], with adjacent sums 3 and 8, for total 9.

Input: ([1, 2, 3, 2], 5)

Expected Output: 8

Explanation: All videos can be selected because adjacent sums are 3, 5, and 5.

Hints

  1. Let dp[i] be the maximum total watch time of a valid subsequence that must end at video i.
  2. 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

You are given a set of videos by duration. A watch session consists of exactly m selections. At each selection, you may choose any video from the list, and the same video may be chosen multiple times. The original feed order does not restrict the session. For every two consecutive selections, the sum of their durations must be at most A. Return the maximum possible total watch time for exactly m selections. If no valid session of exactly m selections exists, return -1. If m is 0, the empty session is valid and has total watch time 0.

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.

Input: ([4, 2, 6, 3, 5], 8, 4)

Expected Output: 16

Explanation: One optimal session is [6, 2, 6, 2], where every adjacent sum is 8.

Input: ([8, 1, 7, 2, 6], 8, 5)

Expected Output: 23

Explanation: One optimal session is [7, 1, 7, 1, 7], with total 23.

Input: ([6, 7], 5, 2)

Expected Output: -1

Explanation: No pair of selections can satisfy the adjacent-sum limit, so a session of length 2 is impossible.

Hints

  1. Use dynamic programming over the session length: dp[t][d] can represent the best total for a length-t session ending with duration d.
  2. 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.
Last updated: Jun 25, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Course Schedule Feasibility - Bytedance (hard)
  • Least Frequently Used (LFU) Cache - Bytedance (hard)
  • SQL: Students Above Average and Passing Every Subject - Bytedance (hard)
  • Determine If One Binary Tree Is a Substructure of Another - Bytedance (hard)
  • Reverse Nodes in K-Sized Groups - Bytedance