Design rolling-window top-K click tracker
AmazonSoftware Engineer · OnsiteLast updated
Design rolling-window top-K click tracker evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
You receive a high-volume stream of click events (timestamp in ms, url). Implement a data structure with two operations:
- record(event) inserts one event;
- queryTopK(T, K, now) returns the K most-clicked URLs in the half-open interval (now − T, now], and queryUnique(T, now) returns the number of distinct URLs in that interval. Requirements: maintain near real-time results with efficient insert and eviction; target O(log K) or better per event for top-K maintenance; ensure memory stays bounded via time-based eviction. Handle out-of-order events with maximum lateness L and deduplicate by an event id for idempotency. Discuss the data structures you would use (e.g., time-bucketed sliding windows, hash maps for counts, heaps for top-K, deques for expirations), analyze time/space complexity, and explain how you would adapt the design for concurrency (multiple producer threads, lock contention minimization, atomicity of updates) and horizontal sharding.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.