I jumped straight to data structures before they even asked me to, which was a mistake.
Start by clarifying requirements and constraints (e.g., asset class, expected throughput, latency, consistency needs). Then propose a clean API surface with clear semantics, and implement using appropriate data structures (e.g., hash maps for order lookup, balanced BST or skip list for price levels). Emphasize consistency, error handling, and extensibility.
Pro tip: Demonstrate awareness of real-world trading system concerns: concurrency, lock-free designs, and the importance of deterministic behavior. Mention that you'd separate the API from the implementation to allow future optimizations.
Ask about expected order volume, latency requirements, concurrency model, and whether the book is single-threaded or multi-threaded. Confirm that matching is out of scope.
Define methods: placeOrder, cancelOrder, getOrder, getBestBid, getBestAsk, getDepth. Specify parameters, return types, and error conditions (e.g., duplicate order ID, unknown order).
Use a hash map for O(1) order lookup by ID. For price levels, use a balanced BST (e.g., TreeMap in Java) or skip list to maintain sorted bids/asks. Each price level holds a list or queue of orders (FIFO).
Implement placeOrder (insert into map and appropriate price level), cancelOrder (remove from map and price level), and query methods. Ensure consistency by updating both structures atomically.
Cover handling of empty book, duplicate IDs, canceling non-existent orders, and concurrency (e.g., using locks or lock-free structures). Mention potential optimizations like intrusive lists or memory pools.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.