From Data Structures and Algorithms

Jane Street Interview

Some Practice Questions, Jane Street Style

Question 1: Matching engine

You are building the core of an exchange for one symbol.

Stage 1 (easy). Accept limit orders (order_id, side, price, quantity) and store them in an order book. Implement print_book(): bids from highest price to lowest, asks from lowest to highest, showing total quantity at each price level.

Answer 1.1 — plan:

txt
OrderBook
- Store a dictionary mapping price -> list of orders

add()
- Adds an order to that price level's list of orders

print_book()


Answer 1.1 — code:

Stage 2 (medium). Add matching. An incoming order that crosses the book (buy price ≥ best ask, or sell price ≤ best bid) executes immediately against resting orders, best price first, oldest first within a price. Trades print as (resting_id, incoming_id, price, quantity) and execute at the resting order’s price. Any unfilled remainder rests in the book.

Answer 1.2 — plan:

Answer 1.2 — code:

Stage 3 (hard). Add cancel(order_id) and modify(order_id, new_price, new_quantity). Cancel must work on partially filled orders and be safe on unknown ids. Modify keeps time priority only if the price is unchanged and the quantity decreased; otherwise it is treated as cancel + new order. State the time complexity of every public operation you now have.

Answer 1.3 — plan:

Answer 1.3 — code:

Answer 1.3 — complexities: