Start by clarifying requirements (exact vs approximate, latency, memory, distributed scale) and then propose a two-tier architecture: a fast in-memory layer for sliding window using bucketed time slots and a persistent layer for all-time counts. Use a min-heap or order-statistic tree for top-K with O(log K) updates, and discuss handling late events via watermarks or allowed lateness. Finally, extend to distributed setup with sharding by branch and merging results, analyzing trade-offs.
Pro tip: Emphasize the trade-off between exactness and scalability: for Coinbase-scale, approximate methods like Count-Min Sketch with a heap often suffice, but be ready to discuss how to achieve exact counts if needed. Also, mention that late events can be handled by maintaining a small buffer and reprocessing, but this adds complexity.
Ask about expected event rate, number of branches, T (window size), K, acceptable latency, and whether exact counts are required. Determine if events can be late and by how much.
For all-time: use a hash map from branch ID to count, and a min-heap of size K for top-K. For sliding window: use time-bucketed counters (e.g., per minute) and a sliding window aggregator; maintain a heap for top-K per window. Ensure updates are O(log K) by using a balanced BST or heap with lazy deletion.
Use event-time processing with watermarks to define when a window is complete. Allow a configurable lateness period; buffer late events and update counts retroactively, possibly recomputing affected top-K. Discuss trade-offs: longer lateness increases memory and complexity.
For high cardinality, exact counts may be infeasible. Propose approximate methods like Count-Min Sketch for frequency estimation and a heap for top-K, or Space-Saving algorithm. Discuss error bounds and memory savings.
Shard by branch ID across nodes; each node maintains local top-K and counts. A coordinator merges results to get global top-K. For sliding window, use distributed time buckets and synchronize watermarks. Analyze time complexity: ingestion O(log K) per event, query O(K) to merge, and space O(B + K) per node.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.