InterviewDB Question · Los Angeles

Exchange Broker: Implement an Order Matching Engine for a Simple Exchange

Question Details

Problem

Build a simplified order matching engine for a single trading pair. Support limit orders (buy/sell at a specific price) and market orders (execute at best available price). Match buy orders against sell orders using price-time priority.

Return a list of fills.

python
class Exchange:
    def place_order(self, order_id: str, side: str, order_type: str,
                    price: float | None, qty: int) -> list[dict]:
        """
        side: 'buy' or 'sell'
        order_type: 'limit' or 'market'

**Returns** list of fills: [{buy_id, sell_id, price, qty}]
        """
        pass

    def cancel_order(self, order_id: str) -> bool: ...
    def get_orderbook(self) -> dict: ...  # {bids: [...], asks: [...]}

Example:

place_order("B1", "buy",  "limit", 100.0, 10)  -> []      # sits in book
place_order("S1", "sell", "limit",  99.0,  5)  -> [{buy:B1, sell:S1, price:100.0, qty:5}]

Follow-ups

  1. Why does a limit buy match against a sell at 99 when the buy was placed at 100?
  2. What data structures back your bid and ask books for O(log n) insert and O(1) best-price lookup?
  3. How do you handle partial fills and track remaining quantity?
  4. Extend to support stop-loss orders — how do they interact with the matching loop?

Full Details

Problem

Build a simplified order matching engine for a single trading pair. Support limit orders (buy/sell at a specific price) and market orders (execute at best available price). Match buy orders against sell orders using price-time priority.

Return a list of fills.

python
class Exchange:
    def place_order(self, order_id: str, side: str, order_type: str,
                    price: float | None, qty: int) -> list[dict]:
        """
        side: 'buy' or 'sell'
        order_type: 'limit' or 'market'

**Returns** list of fills: [{buy_id, sell_id, price, qty}]
        """
        pass

    def cancel_order(self, order_id: str) -> bool: ...
    def get_orderbook(self) -> dict: ...  # {bids: [...], asks: [...]}

Example:

place_order("B1", "buy",  "limit", 100.0, 10)  -> []      # sits in book
place_order("S1", "sell", "limit",  99.0,  5)  -> [{buy:B1, sell:S1, price:100.0, qty:5}]

Follow-ups

  1. Why does a limit buy match against a sell at 99 when the buy was placed at 100?
  2. What data structures back your bid and ask books for O(log n) insert and O(1) best-price lookup?
  3. How do you handle partial fills and track remaining quantity?
  4. Extend to support stop-loss orders — how do they interact with the matching loop?

About This Question

This is a reported interview question from a voleon group interview during the onsite round.

It covers the following topics: Coding, Onsite .