Order Book Final State
MediumHeap ยท Two Sigma
Replay events on an empty limit order book and return the resting orders. Each is ["new", id, side, price, qty] with side "B" or "S", ["cancel", id] or ["amend", id, qty].
- A new order trades while it has quantity left and the best opposing order crosses it (buy price >= sell price). Best is the highest bid or lowest ask, then the earliest in priority. Leftover quantity rests.
amendsets a resting order's quantity. Raising it moves the order behind the others at its price; otherwise it keeps its place.canceloramendof an order that is not resting does nothing.
Return [bids, asks], each a list of [id, price, qty] best first, ties in priority order.
Example 1
- Input:
- events = [["new", "a", "S", 12, 3], ["new", "b", "S", 11, 4], ["new", "c", "S", 11, 2], ["new", "d", "B", 12, 8]]
- Output:
- [[], [["a", 12, 1]]]
- Explanation
- Buy
dtakes 4 fromband 2 fromcat 11, then 2 of the 3 at 12. One unit ofaremains.
Example 2
- Input:
- events = [["new", "a", "B", 20, 5], ["new", "b", "B", 20, 5], ["amend", "a", 7], ["new", "c", "S", 20, 6]]
- Output:
- [[["a", 20, 6]], []]
- Explanation
- Raising
ato 7 sends it behindb, socfills 5 fromband then 1 froma.
Example 3
- Input:
- events = [["new", "a", "B", 9, 4], ["new", "b", "S", 10, 4]]
- Output:
- [[["a", 9, 4]], [["b", 10, 4]]]
Constraints
1 <= events.length <= 10^51 <= price <= 10^91 <= qty <= 10^6for new and amend events- Ids of new orders are distinct strings; cancel and amend may name an id that never appeared