Best Bid Ask Stream
MediumHeaps · Coinbase
You receive a stream of order book updates. Each update is [side, price, size], where side is "bid" or "ask":
- If
size > 0, the price levelpriceon that side now has that size (it is created if missing, or its size is replaced). - If
size == 0, the price level is removed from that side. Removing a level that does not exist does nothing.
After every update, record [best_bid, best_ask], where best_bid is the highest price currently on the bid side and best_ask is the lowest price currently on the ask side. Use None for a side with no levels. The two sides are independent: do not match or remove levels even if the best bid is at or above the best ask.
Return the list of recorded pairs, one per update, in update order.
Example 1
- Input:
- updates = [["bid", 100, 3], ["ask", 104, 2], ["bid", 101, 1], ["ask", 103, 5], ["bid", 101, 0], ["ask", 103, 0]]
- Output:
- [[100, None], [100, 104], [101, 104], [101, 103], [100, 103], [100, 104]]
- Explanation
- The best bid is the highest bid price and the best ask is the lowest ask price. Prices 101 and 103 become best when added; removing them (size 0) brings back 100 and 104.
Example 2
- Input:
- updates = [["ask", 50, 1], ["ask", 50, 4], ["ask", 49, 0]]
- Output:
- [[None, 50], [None, 50], [None, 50]]
- Explanation
- Changing the size of an existing level keeps it as the best ask. Removing a price level that does not exist is a no-op. The bid side stays empty, so it is reported as
None.
Example 3
- Input:
- updates = [["bid", 10, 1], ["bid", 10, 0], ["bid", 10, 2]]
- Output:
- [[10, None], [None, None], [10, None]]
- Explanation
- A level that was removed can be added back later.
Constraints
0 <= updates.length <= 10^5sideis"bid"or"ask"1 <= price <= 10^90 <= size <= 10^9