Enforce Per-Campaign Ad Frequency Caps in a Rolling Window
Last updated: November 29, 2026
Quick Overview
Implement a frequency capper that allows at most N impressions of a campaign per viewer within any rolling W-second window, then use it to pick the highest ranked eligible campaign for an ad request.
Tubi
November 29, 20260
0
0 solved
Implement a frequency capper that allows at most N impressions of a campaign per viewer within any rolling W-second window, then use it to pick the highest ranked eligible campaign for an ad request.
Tubi's in-house ad server must stop the same advertiser from repeating too often during a long viewing session, which annoys viewers and wastes advertiser budget. The onsite coding round uses this to test sliding-window data structures and how the candidate thinks about memory and distribution in an ad request path.
What the Interviewer Expects
- Uses a rolling window, not fixed clock buckets, and explains the difference
- Evicts expired timestamps so memory stays bounded by the cap
- Only records an impression for the campaign actually chosen
- Gets the window boundary condition right and states it
- Discusses how the state would live in a shared store across servers
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
- How would you implement this across many ad servers using Redis or ScyllaDB?
- Impressions are confirmed by a later beacon, not at decision time. How does that change the design?
- How would you add a cap at the advertiser level that spans several campaigns?
- What approximation would you accept to cut memory for hundreds of millions of viewers?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
try_serve(user, campaign, ts) returns True and records an impression if fewer than cap impressions occurred in (ts - window, ts], else False. Timestamps per key arrive non-decreasing. pick_ad ...
High-Level Design
Keep a deque of impression timestamps per (user, campaign). On each check, pop from the left while the oldest is outside the window, then compare length with the cap. Each deque never holds more than ...