Given a high-volume stream of events (e.g., account IDs from new account openings), design and implement a data structure that supports:
(
-
inserting an item,
(
-
returning the current top-K most frequent items, and
(
-
optionally returning the top-K over the last T minutes (sliding window). Discuss algorithm choices (heaps, bucket counting, count–min sketch with heap), time/space complexities, handling ties, changing K at query time, and window expiration. Outline a scalable distributed approach, including partitioning, partial aggregation, and merge semantics.