Sliding Window Counters And QPS
Asked of: Software Engineer
Last updated

What's being tested
These problems test time-windowed aggregation: maintaining counts, rates, or averages over the last seconds without scanning all historical events. Interviewers look for clean data structure tradeoffs, correct expiry logic, and complexity analysis under monotonic timestamps, timestamp collisions, and high event volume.
Patterns & templates
-
Circular bucket array — store
(timestamp, count)per second;hit(t)andget(t)areO(1)/O(W), spaceO(W). -
Lazy bucket reset — when
t % Wis reused, reset bucket if stored timestamp differs; prevents stale counts from leaking. -
Deque of timestamps/events — append on hit, pop expired while
front <= now - W; amortizedO(1), space proportional to recent hits. -
Aggregated deque buckets — store
(bucketStart, count)for sparse streams or range queries; merge same bucket, evict old buckets. -
Running total optimization — maintain
totalalongside buckets/deque sogetCount()isO(1)after evicting expired entries. -
QPS formula — average QPS is
events_in_window / window_seconds; clarify whether denominator is fixedWor elapsed warm-up time. -
Per-key counters — use
Map<Key, Counter>for KV-store variants; evict inactive keys if memory bounds matter.
Common pitfalls
Pitfall: Forgetting timestamp collisions in modulo buckets;
t % Walone is not enough without storing the bucket’s real timestamp.
Pitfall: Off-by-one expiry errors; define whether the valid interval is
(now - W, now]or[now - W, now].
Pitfall: Claiming
O(1)queries while summing allWbuckets each time; either admitO(W)or maintain a running total.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Featured in interview prep guides
Practice questions
- Design KV store with sliding-window average QPSDatabricks · Software Engineer · Technical Screen · medium
- Implement a rate-limited hit counterDatabricks · Software Engineer · Onsite · medium
- Compute last-5-minute QPS in memoryDatabricks · Software Engineer · Technical Screen · medium
- Design Tic-Tac-Toe and QPS data structuresDatabricks · Software Engineer · Onsite · medium
- Implement a sliding-window hit counterDatabricks · Software Engineer · Technical Screen · Medium
- Design a rolling event tracker with rangesDatabricks · Software Engineer · Technical Screen · Medium
Related concepts
- Intervals, Sliding Windows, And Time-Ordered StateCoding & Algorithms
- Sliding Window And Streaming StatisticsCoding & Algorithms
- Sliding Window Frequency MapsCoding & Algorithms
- Arrays, Sliding Windows, DP And Stack PatternsCoding & Algorithms
- Real-Time Top-K And Streaming AnalyticsSystem Design
- Sliding Window And K-Flip OptimizationCoding & Algorithms