Compute BBO and NBBO from order data
CitadelSoftware Engineer · Technical ScreenLast updated
This question evaluates a candidate's skill in designing efficient data structures and algorithms for aggregating and querying order-book data, including computing best bid/ask prices and summing quantities at those prices.
You are given a list data of order records. Each record is a 4-tuple:
(exchange_id, price, quantity, order_type)
exchange_id: string/int identifying the exchangeprice: positive numberquantity: positive integerorder_type: either"B"(bid/buy) or"A"(ask/sell)
Implement a class QuoteBook initialized with data, supporting:
get_exchange_bbo(exchange_id) -> (best_bid, best_ask)
- Returns the best bid and best ask for that exchange.
get_nbbo() -> (best_bid, best_ask)
- Returns the national best bid and offer (NBBO) across all exchanges.
Definitions:
- Best bid: highest bid price.
- Best ask: lowest ask price.
- If there are multiple orders at the same best price on the relevant scope (exchange or all exchanges), treat the displayed quantity as the sum of quantities at that best price.
- If a side does not exist (no bids or no asks), return
Nonefor that side.
Return format:
best_bidandbest_askshould each be eitherNoneor a tuple(price, total_quantity_at_price).
Constraints/expectations:
len(data)can be large (e.g., up to 10^5 or more), so repeated calls to these methods should be efficient.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.