I went with a sorted structure per exchange, like a max-heap for bids and min-heap for asks, which felt right until they pushed on cancellations.
Start by clarifying requirements and assumptions, then propose a data structure that supports efficient per-exchange and global best bid/ask queries. Walk through the design, analyze time and space complexity, and discuss how to handle cancellations and quantity updates. Finally, mention potential optimizations and trade-offs.
Pro tip: Emphasize the importance of maintaining both per-exchange and global order books, and discuss how to handle updates efficiently without full recomputation. Showing awareness of real-world constraints like message ordering and latency will impress interviewers.
Ask about expected message volume, latency requirements, and whether orders can be modified or canceled. Clarify if the stream is ordered and if exchanges are known in advance.
Propose a per-exchange order book using two heaps (max-heap for bids, min-heap for asks) or balanced BSTs for efficient best price retrieval. For global best, maintain a heap of best bids and asks across exchanges, updating lazily or eagerly.
Discuss time complexity for insertions, cancellations, and best price queries. For heaps, insertion is O(log n), best is O(1), but cancellation requires lazy deletion. For BSTs, all operations are O(log n).
Explain how to handle cancellations: use a hash map to track order IDs and mark them as canceled, then lazily remove from heaps when they reach the top. For quantity updates, treat as cancel and reinsert, or update in place if using a balanced BST.
Compare heap vs. balanced BST approaches, considering memory and update frequency. Mention possible optimizations like maintaining a global heap with lazy updates, or using a skip list for concurrent access.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.