KatInterview

Aggregated Multi-Venue Liquidity Tracker with BBO and NBBO

Data Structure DesignAuto-graded · 22 test cases
MediumAuto-gradedData Structure Designheapsdata-structureslazy-deletion

Last updated

Build a system that maintains liquidity across trading venues and efficiently queries best prices per venue and across all venues.

Build an in-memory liquidity tracking system that consolidates order information from multiple trading venues. Each liquidity record specifies an exchange identifier, a price level, a quantity, and whether it represents buy interest (bid) or sell interest (ask).

Your system must provide three capabilities:

  1. add(exchange, price, quantity, side) — Update the total quantity available at a specific price level on one side of one exchange. The quantity parameter represents a signed delta: positive values increase liquidity, negative values reduce it.

  2. get_exchange_bbo(exchange) — Retrieve the best bid and offer for a single exchange, meaning the highest bid price and the lowest ask price currently available on that venue.

  3. get_nbbo() — Determine the national best bid and offer across all exchanges: the single highest bid price and single lowest ask price from any venue.

Implement these operations using only Python's standard library (no external packages like sortedcontainers), and analyze their time complexity.

Rules and Edge Cases

  • Liquidity is tracked per (exchange, side, price) tuple. Multiple updates to the same tuple accumulate.
  • When a price level's total quantity reaches zero, that level is removed from the book and cannot be returned as a best price.
  • If one side of an exchange has no active levels, return None for that side's best price.
  • Quantity updates are cumulative. If applying a delta would cause a level's total to drop below zero, raise a ValueError with the message "quantity cannot become negative".
  • An exchange that has never received any updates should return [None, None] for its BBO.
  • You must use only the heapq module from Python's standard library for heap operations. Do not import or use sortedcontainers, blist, or any third-party sorted data structures.

Constraints

  • Exchange names are non-empty strings.
  • Prices are positive floating-point numbers.
  • Quantities are non-zero integers (positive or negative deltas).
  • Side is either the string "bid" or "ask".
  • Up to 10,000 total operations.
  • Up to 100 distinct exchanges.
  • Up to 1,000 distinct price levels per exchange per side.

Examples

Example 1

Input: execute_order_book(['add', 'add', 'add', 'get_exchange_bbo', 'get_nbbo'], [['NYSE', 100, 10, 'bid'], ['NYSE', 102, 5, 'ask'], ['NASDAQ', 101, 7, 'bid'], ['NYSE'], []])

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

Example 2

Input: execute_order_book(['add', 'add', 'add', 'add', 'get_nbbo', 'add', 'get_nbbo'], [['A', 10, 4, 'bid'], ['A', 12, 3, 'ask'], ['B', 11, 8, 'bid'], ['B', 13, 2, 'ask'], [], ['A', 10, -4, 'bid'], []])

Output: [None, None, None, None, [11, 12], None, [11, 12]]

Hints

Hint 1

Use a heap per side per exchange to track the best price, but remember that cancelled levels leave stale entries behind

Hint 2

Maintain a global heap for each side that stores entries from all exchanges, but verify each entry is still current before trusting it

Hint 3

When an exchange's best price changes, push a new versioned entry into the global heap instead of updating in place