Schedule Unit-Length Ads Before Deadlines to Maximize Revenue

Last updated: November 29, 2026

Quick Overview

A TikTok TopView inventory has slots 1, 2, 3 and so on, each holding one ad. Each candidate ad has a revenue and a deadline slot it must run by; choose ads and slots to maximize total revenue.

TikTok
Coding & Algorithms
Software Engineer
TikTok
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


A TikTok TopView inventory has slots 1, 2, 3 and so on, each holding one ad. Each candidate ad has a revenue and a deadline slot it must run by; choose ads and slots to maximize total revenue.

Ads scheduling questions show up in TikTok onsites for ads infrastructure teams, and this one is the classic deadline scheduling greedy in disguise. The interviewer wants a correct greedy, a clear exchange argument, and attention to complexity when there are millions of candidates.

What the Interviewer Expects
  • Restates the problem as job sequencing with deadlines and unit durations
  • Processes by deadline and keeps the best revenues in a min-heap
  • Explains the exchange argument for why dropping the minimum is optimal
  • O(n log n) without simulating each slot
  • Mentions the alternative: sort by revenue and place in latest free slot with union-find
Key Topics to Cover
greedy
heaps
exchange argument
scheduling
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 the actual slot for each chosen ad?
  • Ads now take different numbers of slots; is greedy still optimal?
  • Advertisers also require a minimum start slot; what changes?
  • How would pacing and frequency caps interact with this optimizer in practice?
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

Each ad takes exactly one slot. An ad with deadline d may run in any slot 1..d. At most one ad per slot. Maximize total revenue; return the sum (and optionally the schedule). Revenues are positive....

High-Level Design

Sort ads by deadline. Sweep and push each ad's revenue onto a min-heap of selected ads. The invariant: after processing all ads with deadline at most d, the heap is the most valuable feasible set amon...


Submit Your Answer
Markdown supported