Maintain Price Levels For A Single-Symbol Order Book
Implement a simplified NASDAQ market data book for a single symbol. The system receives callbacks for order insertions, modifications, and cancellations. Each order is defined by an ID, side ('buy' or 'sell'), price, and quantity. The core requirement is to implement a get_price_level(side, level_index) function that returns the price and the aggregated quantity for a specific price level. Level 0 represents the best price: the highest price for buy orders and the lowest price for sell orders.
Function Signature:
class OrderBook:
def on_order_insert(self, order_id: int, side: str, price: int, qty: int) -> None:
pass
def on_order_modify(self, order_id: int, price: int, qty: int) -> None:
pass
def on_order_cancel(self, order_id: int) -> None:
pass
def get_price_level(self, side: str, level_index: int) -> tuple[int, int] | None:
pass
Constraints:
sideis either'buy'or'sell'.- Order IDs are unique for active orders.
- Modifications can alter both price and quantity.
- Canceling an unknown order should be handled gracefully.
- If a requested price level does not exist, return
None.
Examples:
- Insert buy order ID 1 (price 100, qty 5); insert buy order ID 2 (price 101, qty 3).
get_price_level('buy', 0)should return(101, 3). - Insert sell order ID 3 (price 105, qty 2); insert sell order ID 4 (price 105, qty 7).
get_price_level('sell', 0)should return(105, 9).
AI interview prep
Core dilemma
Efficiently managing and querying a dynamic, multi-level price book to provide aggregated quantities at specific price points, while handling frequent updates.
3 tradeoffs and 2 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Maintain Price Levels For A Single-Symbol Order Book
Implement a simplified NASDAQ market data book for a single symbol. The system receives callbacks for order insertions, modifications, and cancellations. Each order is defined by an ID, side ('buy' or 'sell'), price, and quantity. The core requirement is to implement a get_price_level(side, level_index) function that returns the price and the aggregated quantity for a specific price level. Level 0 represents the best price: the highest price for buy orders and the lowest price for sell orders.
Function Signature:
class OrderBook:
def on_order_insert(self, order_id: int, side: str, price: int, qty: int) -> None:
pass
def on_order_modify(self, order_id: int, price: int, qty: int) -> None:
pass
def on_order_cancel(self, order_id: int) -> None:
pass
def get_price_level(self, side: str, level_index: int) -> tuple[int, int] | None:
pass
Constraints:
sideis either'buy'or'sell'.- Order IDs are unique for active orders.
- Modifications can alter both price and quantity.
- Canceling an unknown order should be handled gracefully.
- If a requested price level does not exist, return
None.
Examples:
- Insert buy order ID 1 (price 100, qty 5); insert buy order ID 2 (price 101, qty 3).
get_price_level('buy', 0)should return(101, 3). - Insert sell order ID 3 (price 105, qty 2); insert sell order ID 4 (price 105, qty 7).
get_price_level('sell', 0)should return(105, 9).