← Two Sigma Interview Insights
I knew the general idea going in: max heap for buys, min heap for sells, match when best bid crosses best ask.
Start by clarifying requirements: order types, matching rules, output format, and performance constraints. Then outline a design using a price-time priority order book (e.g., two heaps or sorted maps) and walk through the matching logic. Finally, discuss implementation details, edge cases, and trade-offs.
Pro tip: Emphasize the importance of data structure choice for achieving O(log n) insertion and O(1) best price retrieval, and mention how you would handle partial fills and order cancellation.
Ask about order types (limit/market), matching rules (price-time priority), output format (fills or order book), and any performance constraints.
Propose using two priority queues (max-heap for bids, min-heap for asks) or balanced BSTs to maintain price-time priority efficiently.
Describe the step-by-step process: when a new order arrives, match against opposite side while prices cross, generating fills and updating quantities.
Cover handling partial fills, order cancellation, and outputting the order book state. Mention time complexity for operations.
Talk about edge cases like self-trading, market orders, and discuss trade-offs between different data structures (e.g., heaps vs. sorted lists).
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.