The rolling window part is where I got a bit tangled.
Start by clarifying requirements: what is the cap (e.g., 3 ads per hour), window size, and acceptable latency. Then design a distributed system using a time-series data store (e.g., Redis sorted sets or a sliding window counter) to track ad views per user, and detail the read path (checking if cap exceeded) and write path (recording a view) with consistency and scalability in mind.
Pro tip: Emphasize the trade-off between accuracy and performance: exact rolling windows require storing individual timestamps, while approximate methods (e.g., sliding window counters with buckets) reduce memory but may slightly over- or under-count. Netflix likely values scalability and low latency, so propose a hybrid approach.
Ask about the cap definition (e.g., max ads per user per hour), window type (rolling vs. fixed), expected scale (users, ads per second), latency requirements, and consistency needs (strict vs. eventual).
Select a data store that supports efficient time-based queries, such as Redis sorted sets (timestamp as score) or a sliding window counter using multiple buckets. Consider sharding by user ID for scalability.
On ad request, query the store to count events within the rolling window. If count < cap, allow ad; else, deny. Use caching or read replicas to reduce latency, and handle eventual consistency if using distributed counters.
When an ad is shown, record the event with timestamp. For sorted sets, add member with current timestamp and remove old entries (e.g., older than window). For bucket counters, increment the current bucket and optionally expire old buckets.
Discuss partitioning, replication, and failure handling. Compare exact vs. approximate counting, and explain how to handle high throughput (e.g., write batching, async processing) while maintaining low latency for reads.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.