KatInterview

Order Book Simulator with Fair Volume Participation

Data Structure DesignModel answer
MediumModel answerData Structure Designorder-bookpriority-queuesimulation

Last updated

Build a limit order matching engine, then add participation-rate execution logic for one client, and finally extend to multiple clients with fair allocation.

Build a trading simulator in three progressive stages.

Stage 1: Limit Order Book

Create a matching engine for a single instrument that processes two event types:

  • NEW orderId side price qty — submits a new limit order where side is either B (buy) or S (sell)
  • CANCEL orderId — removes any unfilled portion of the specified order

Matching behavior:

When a new order arrives, match it immediately against opposing resting orders using:

  1. Price priority — best available price executes first (highest for buys, lowest for sells)
  2. Time priority — among orders at identical prices, earlier submissions execute first

Each match creates a trade where:

  • Trade price equals the resting order's limit price
  • Trade quantity equals the minimum of what the incoming order needs and what the resting order offers

Required output: For each input event, return all trades generated (empty list if none).


Stage 2: Volume Participation for One Client

A client submits an order specifying:

  • Target quantity Q to execute
  • Participation rate p (where 0 < p ≤ 1)

The market generates a sequence of volume events:

  • PRINT t marketQty — indicates that marketQty shares traded at time t

Your execution algorithm must satisfy this constraint at all times:

clientExecutedSoFar ≤ floor(p × cumulativeMarketVolume)

Required output: For each print event, determine how many shares to execute for the client (may be zero), continuing until either Q is reached or the print stream ends.


Stage 3: Multiple Clients with Fair Allocation

Extend to handle multiple simultaneous client orders, each with its own (Q_i, p_i).

At each print event, allocate executions across all clients satisfying:

  1. Each client's execution respects executed_i ≤ floor(p_i × cumulativeMarketVolume)
  2. No client executes beyond their remaining quantity
  3. Total allocation at each print cannot exceed that print's marketQty
  4. When available volume is insufficient for all allowable executions, prioritize clients by smallest fill ratio:
executed_i / Q_i

Break ties by choosing the smaller clientId.

Implementation requirements:

  • Clearly state your assumptions about edge cases
  • Handle scenarios like canceling non-existent orders or participation rates allowing more than the remaining target
  • Use exact arithmetic where precision matters

Hints

Hint 1

For the order book, consider using heaps with sequence numbers to enforce time priority within each price level

Hint 2

The participation constraint is cumulative across all prints—executing more now never reduces future allowance

Hint 3

When multiple clients compete for limited volume, a min-heap tracking fill ratios naturally implements the fairness requirement