Fewest Stock Clips Needed to Fill an Exact Ad Duration

Last updated: November 29, 2026

Quick Overview

TikTok's ad creative tool offers stock clips in a set of fixed durations (in seconds), each available unlimited times. Given a target ad length, return the minimum number of clips whose durations sum exactly to the target, or -1 if impossible.

TikTok
Coding & Algorithms
Software Engineer
TikTok
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


TikTok's ad creative tool offers stock clips in a set of fixed durations (in seconds), each available unlimited times. Given a target ad length, return the minimum number of clips whose durations sum exactly to the target, or -1 if impossible.

Tools like TikTok's ad creative suite assemble templates from reusable clips, and this unbounded knapsack framing is a common medium DP question in technical screens. Interviewers watch for candidates who try a greedy largest-clip-first approach and whether they can show a counterexample.

What the Interviewer Expects
  • Shows that greedy largest-first fails with a concrete counterexample
  • Defines dp[t] as the fewest clips summing to t
  • Initializes unreachable states with infinity and dp[0] = 0
  • Runs in O(target * number of durations)
  • Discusses BFS as an equivalent view and reconstructing the clip list
Key Topics to Cover
unbounded knapsack
1D dynamic programming
coin change
greedy counterexample
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 which clips to use?
  • How would you count the number of distinct combinations instead?
  • What if each clip duration can be used at most once?
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: durations: list[int] (positive), target: int >= 0.
  • Output: minimum clip count, or -1.
  • Order of clips does not matter for the count....
High-Level Design

Greedy fails: durations [1, 3, 4], target 6. Greedy takes 4 + 1 + 1 (3 clips), but 3 + 3 is 2 clips.

DP: dp[t] = 1 + min(dp[t - d]) over durations d <= t, with dp[0] = 0. Fill from 0 to targe...


Submit Your Answer
Markdown supported