~/problems

Problems

Basics first, then the classics, then company-style assessments. Every problem has tests you run right here; multi-level ones unlock as you go. See the roadmap.

Trading systems

Order books and matching engines

Price levels, price-time priority, partial fills, cancels.

Notes

Recognise it when: limit and market orders, bids and asks, "best bid/offer", matching, cancels, price-level aggregation from a market-data feed.

  • Book structure: for each side, a map of price -> deque of orders (FIFO within a price gives time priority), plus sorted price levels. In Python, use heaps with lazy deletion or bisect on sorted price lists.
  • Matching a buy limit at price P: while the best ask ≤ P and quantity remains, fill against the front order at the best ask (the trade happens at the resting order's price). Pop filled orders, and remove empty levels. Any remainder rests on the bid side.
  • Market order: match at any price, and never rest.
  • Cancel / modify: keep an order_id -> order index. Reducing quantity usually keeps queue priority, while increasing it or changing the price loses priority (cancel and re-add).
  • A price-level book from a feed (add/modify/delete messages): aggregate quantity per price, and report the top N levels.

Gotchas: crossing your own order, zero-quantity leftovers, ties at the same price (FIFO), and floats for prices (use integer ticks).

6 problems

Quant & trading

Trading systems Order books, matching engines, market simulations.

Order books and matching engines

esc