
Given a stream of account-opening events (branch_id, user_id, timestamp), design algorithms and data structures to compute the top-K branches by number of openings for (a) all time and (b) a sliding window of the last T minutes. Support event ingestion in near real time, queries at any time, and updates in O(log K) or better. Address late or out-of-order events, memory usage, and trade-offs between exact and approximate methods (e.g., heaps + hash maps vs. sketches). Extend your design to a distributed environment and analyze time and space complexity.