← 返回 citadel 的题目列表Design an Order Book Class to Compute Exchange BBO and NBBO
类型:online_judge
You are given a stream of order records. Each record has:
exchange_id: exchange identifier (string)
price: price (integer)
quantity: quantity (positive integer)
order_type: side ("bid" or "ask")
Implement a class OrderBook that supports:
add(exchange_id, price, quantity, order_type): add one order record.
get_exchange_bbo(exchange_id): return that exchange’s current BBO (Best Bid and Offer) as (best_bid, best_ask).
best_bid is the highest bid price on that exchange, or None if no bids
best_ask is the lowest ask price on that exchange, or None if no asks
get_nbbo(): return the market-wide NBBO as (best_bid, best_ask).
best_bid is the maximum of all exchanges’ best_bid, or None if no bids exist anywhere
best_ask is the minimum of all exchanges’ best_ask, or None if no asks exist anywhere
Notes/Constraints:
quantity does not affect best price selection in this problem, but should be accepted/stored (for extensibility).
Scale:
up to N = 2 * 10^5 calls to add
up to E = 10^4 exchanges
Queries should be efficient; avoid scanning all historical orders on every query.
Suggested I/O format for evaluation:
Input:
First line integer Q (number of operations)
Next Q lines are one of:
ADD exchange_id price quantity order_type
EXBBO exchange_id
NBBO
Output:
For each EXBBO and NBBO, print one line: best_bid best_ask (space-separated). Use string None for missing.
Example
Input
10
ADD NASDAQ 100 10 bid
ADD NASDAQ 101 5 ask
EXBBO NASDAQ
NBBO
ADD NYSE 99 7 bid
ADD NYSE 102 2 ask
NBBO
ADD NASDAQ 98 1 bid
ADD NASDAQ 97 1 ask
NBBO
Output
100 101
100 101
100 101
100 97