Place Ads in a Feed to Maximize the Smallest Gap
Last updated: November 29, 2026
Quick Overview
A feed of upcoming videos has a sorted list of candidate positions where an ad may be inserted. Choose exactly m positions so that the minimum distance between any two chosen ads is as large as possible, and return that distance.
TikTok
November 29, 20260
0
0 solved
A feed of upcoming videos has a sorted list of candidate positions where an ad may be inserted. Choose exactly m positions so that the minimum distance between any two chosen ads is as large as possible, and return that distance.
Spacing ads evenly protects user experience in TikTok's For You feed, and this online assessment problem is a standard binary search on the answer. The grader checks the greedy feasibility test and the upper-bound search pattern.
What the Interviewer Expects
- Recognizes that larger minimum gaps are harder to satisfy, so feasibility is monotonic
- Uses a greedy check that places each ad at the earliest position at least d after the last
- Searches for the largest feasible d
- Sorts positions if not already sorted
- Handles m = 1 and m equal to the number of positions
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- What if some positions have higher revenue and you must also maximize revenue?
- How would you place ads in a live feed where positions arrive one by one?
- What if there must also be a minimum number of organic videos before the first ad?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input:
positions: list[int](distinct),mwith2 <= m <= len(positions). - Output: maximum possible minimum gap.
- For m = 1 the gap is undefined; I will return 0....
High-Level Design
If we can place m ads with minimum gap d, we can also do it for any smaller d. So binary search the largest feasible d in [1, (last - first)].
Check(d): place the first ad at the first position, th...