Rolling-Window Frequency Cap for Household Ad Delivery

Last updated: November 29, 2026

Quick Overview

Implement should_serve(ts, household, campaign) that returns True and records an impression only if that household has seen fewer than the campaign's cap within the trailing window_seconds; requests arrive in timestamp order.

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

0

0

0 solved


Implement should_serve(ts, household, campaign) that returns True and records an impression only if that household has seen fewer than the campaign's cap within the trailing window_seconds; requests arrive in timestamp order.

Frequency capping is a core rule in Roku's ad serving: advertisers buy reach across households and do not want one living room to see the same spot ten times in an evening. The interviewer is checking sliding-window reasoning, exact window boundaries, and awareness of memory growth across millions of households.

What the Interviewer Expects
  • Clarifies whether the window is trailing and whether the boundary is inclusive
  • Counts only served impressions, not requests
  • Uses a per-key deque and evicts expired timestamps lazily
  • Handles campaigns with no cap
  • Discusses memory bounds and how to shard or approximate at scale
Key Topics to Cover
Sliding window
Deque
Hash maps
Ad serving
Distributed counters
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 enforce the cap when ad decisions run on many servers concurrently?
  • How would you support a cap of 3 per day and 10 per week at the same time?
  • Can you reduce memory if caps are large, for example with time buckets?
  • What should happen if the cap store is unavailable?
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 campaign has (max_impressions, window_seconds). For a request at ts, count served impressions for that (household, campaign) in (ts - window, ts]. Serve if the count is below the cap, and...

High-Level Design

Keep a deque of served timestamps per key. On each request, pop from the left while the oldest is outside the window, then compare the length with the cap. Because time only moves forward, each timest...


Submit Your Answer
Markdown supported

Related Questions