Spent a bit of time upfront clarifying what they actually wanted before touching any code, which felt right.
Start by clarifying requirements (e.g., asset type, order types, throughput, latency) and then design a data structure that supports efficient add, cancel, and match operations. Implement the core matching engine using price-time priority, and discuss trade-offs between different data structures and concurrency models.
Pro tip: Emphasize the importance of deterministic behavior and low-latency, and mention how you would handle high availability and fault tolerance, as these are critical in trading systems.
Ask about expected order types (limit, market, etc.), throughput, latency requirements, and whether the system needs to support multiple assets. This ensures the design meets the actual needs.
Choose appropriate data structures for the order book, such as a combination of a hash map for order lookup and a sorted structure (e.g., balanced BST, skip list, or array of price levels) for each side. Explain how to maintain price-time priority.
Describe the matching algorithm: when a new order arrives, match against the opposite side while prices cross, following price-time priority. Handle partial fills and order cancellations efficiently.
Discuss how to handle concurrent access (e.g., single-threaded event loop, locking, lock-free structures) and optimize for low latency. Mention techniques like memory pooling and cache-friendly data layouts.
Compare alternative designs (e.g., using a heap vs. sorted list) and their trade-offs in terms of time complexity, memory, and ease of implementation. Also mention how to extend the system for features like market data publishing and persistence.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.