Exchange BBO and Market-Wide NBBO
Problem statement
Process order-book commands:
ADD exchange side price quantity: add positive quantity to that price level, aggregating with its current quantity.SET exchange side price quantity: replace that level quantity with the given nonnegative quantity. Zero removes the level; removing an absent level does nothing.BBO exchange: return its highest BID and lowest ASK asprice@quantity,price@quantity.NBBO: return the market highest BID and lowest ASK asexchange:price@quantity,exchange:price@quantity.
Side is BID or ASK. Use NA for a missing side. At equal market prices, choose the lexicographically smallest exchange; return that exchange's own level quantity, without summing across exchanges. Zero-quantity levels never supply a quote. Commands start from empty books. Return only BBO and NBBO results in command order.
SET is the practice interface for the source's zero-quantity cleanup follow-up. It is an absolute quantity update, while ADD keeps its additive behavior.
Function
orderBookQuotes(operations: String[]) → String[]Examples
Example 1
operations = ["ADD X BID 100 3","ADD X ASK 105 2","BBO X","NBBO"]return = ["100@3,105@2","X:100@3,X:105@2"]X supplies both local and national best quotes.
Example 2
operations = ["ADD X BID 99 2","ADD Y BID 101 4","ADD X ASK 106 1","ADD Y ASK 104 5","NBBO"]return = ["Y:101@4,Y:104@5"]Y has both the highest bid and lowest ask.
Example 3
operations = ["ADD X BID 100 2","ADD X BID 100 7","BBO X","BBO Z"]return = ["100@9,NA","NA,NA"]Same-price quantity aggregates; Z has no orders.
Example 4
operations = ["ADD X BID 100 3","ADD Y BID 99 2","SET X BID 100 0","NBBO","SET X BID 101 4","NBBO","SET X BID 101 1","BBO X"]return = ["Y:99@2,NA","X:101@4,NA","101@1,NA"]Removing X's price-100 bid exposes Y's bid at 99. Setting a new X bid at 101 makes X best again. The final SET replaces quantity 4 with 1 rather than adding to it.
Constraints
1 <= operations.length <= 100000.- Commands have exactly the documented fields, separated by one space.
- Exchange names have 1 through 32 ASCII letters, digits, or underscores and are compared case-sensitively.
- Prices are integer decimal text from 1 through 1000000000.
- ADD quantities are integers from 1 through 1000000000; SET quantities are integers from 0 through 1000000000.
- Integer text has no plus sign or leading zeros. Total quantity at a level is at most 100000000000000.