Skip to main content
hard

Robinhood Order Book

Also known as: Order Book with Price-Time Priority

Robinhood Order Book (Part 1) Store limit orders and query best bid/ask. DO NOT match orders in Part 1. add_limit_order(id, side, price, qty) -> str (fills, or "" if none) get_best_bid() -> str ("null" or price) get_best_ask() -> str ("null" or price)

Implement an order book with limit/market orders, price-time priority matching, partial fills, and cancellation. Outputs are deterministic and easy to audit.

DesignSimulation

Examples

You write

OrderBook: add_limit_order(order_id: str, side: str, price: int, qty: int), get_best_bid(), get_best_ask()

  1. Example 1

    Input
    OrderBook: addLimitOrder("S1","sell",100,5), addLimitOrder("B1","buy",110,2) (no matching), bestAsk=100, bestBid=110
    returns
    ["","","100","110"]
  2. Example 2

    Input
    Multiple resting orders: bestAsk=99, bestBid=105 (no matching)
    returns
    ["","","","","99","105"]

The pattern behind it

Design

I reach for it when

  • The statement describes a class with methods, not one function.
  • Each method needs to be fast, and the structure decides how fast.

Say this before you type. Say each method, the cost you want from it, and the structure that earns that cost.

All 27 Design problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1Order Storagemedium
2Part 2
3Part 3
4Part 4

Each locked part opens in the editor once the part before it passes.