This question evaluates understanding of streaming data processing, aggregation and ranking at scale, algorithmic complexity, and system-design concerns such as handling late or out-of-order events and distributed scalability.

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.