KatInterview

Simulate a Price-Level Tracker for Buy and Sell Orders

Data Structure DesignAuto-graded · 17 test cases
MediumAuto-gradedData Structure Designheaphash-maplazy-deletion

Last updated

Tests your ability to combine a hash map with a lazy-deletion heap to track best bid and ask prices across a sequence of order book operations.

Problem

You are building the core of a single-instrument trade ledger. The ledger holds open orders on two sides — buyers and sellers — and must answer price queries at any time.

Each order carries four attributes:

  • order_id — a unique string identifier
  • side — either "BUY" or "SELL"
  • price — a positive number (float)
  • quantity — a positive integer

You will receive a sequence of operation names paired with their argument lists. Process every operation in order and collect the return value of each one into a result list.

Operations

add_order(order_id, side, price, quantity)
Records a new order. Multiple orders may rest at the same price. Returns None.

cancel_order(order_id)
Removes the order with the given order_id from the ledger. If no such order exists, do nothing. Returns None.

get_best_bid()
Returns the highest price among all live "BUY" orders, or None if no buy orders are present. Arguments list is empty for this operation.

get_best_ask()
Returns the lowest price among all live "SELL" orders, or None if no sell orders are present. Arguments list is empty for this operation.

Input format

operations: List[str]   — operation names in call order
arguments:  List[List]  — parallel list of argument lists

Return a list of the same length as operations. Each element is the return value of the corresponding call.

Constraints

  • 1 <= len(operations) <= 10^4
  • Every order_id passed to add_order is unique across all calls.
  • cancel_order may be called with an order_id that was never added; treat this as a no-op.
  • get_best_bid and get_best_ask are called with an empty argument list.
  • price is a positive float; quantity is a positive integer.
  • Operations are limited to the four names above.

Examples

Example 1

Input: simulate_order_ledger(['add_order', 'add_order', 'add_order', 'get_best_bid', 'get_best_ask', 'cancel_order', 'get_best_bid'], [['1', 'BUY', 100, 10], ['2', 'BUY', 101, 5], ['3', 'SELL', 105, 20], [], [], ['2'], []])

Output: [None, None, None, 101, 105, None, 100]

Example 2

Input: simulate_order_ledger(['get_best_bid', 'get_best_ask', 'cancel_order'], [[], [], ['missing']])

Output: [None, None, None]

Hints

Hint 1

You need O(1) lookup by order_id for cancellations and O(1) access to the extreme price on each side — no single structure gives you both. Think about pairing a dict with a heap.

Hint 2

A min-heap gives you the lowest price instantly. For the highest buy price, store prices negated so the same min-heap machinery finds the maximum.

Hint 3

When a cancellation empties a price level, you don't have to fix the heap right away. Instead, keep a counter of how many live orders sit at each price and skip over any heap entry whose counter has dropped to zero when you query — this is lazy deletion.