Apply delta updates to an order book snapshot and return the sorted book
Company: Optiver
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Other
You are building a real-time order book feed for an exchange. The book is given as an initial `snapshot`, which is a list of `[price, size]` pairs representing the resting liquidity at each price level. You then receive a stream of `updates`, where each update `[price, size]` is a delta applied to the book:
- If `price` does not exist in the book and `size > 0`, insert a new level `[price, size]`.
- If `price` already exists in the book and `size > 0`, replace its size with the new `size`.
- If `size == 0`, remove the level at `price` from the book (if present).
After applying all `updates` in order, return the final book as a list of `[price, size]` pairs sorted by `price` in ascending order.
### Function Signature
```python
def apply_updates(snapshot: list[list[int]], updates: list[list[int]]) -> list[list[int]]:
...
```
### Rules
- Every level and every update is a two-element list `[price, size]`.
- Each price appears at most once in `snapshot`. The snapshot is not necessarily sorted.
- An update with a positive size sets the level's size to that value. It does not add to the old size.
- An update with size `0` for a price that is not in the book changes nothing.
- Updates are applied in the order given, so when several updates touch the same price, the last one decides its final state.
- Return each remaining level as a `[price, size]` list, sorted by price ascending. Prices in the book are distinct, so this order is unique. If the book ends up empty, return `[]`.
### Constraints
- `1 <= len(snapshot) <= 10^5`
- `1 <= len(updates) <= 10^5`
- Each `snapshot[i] = [price, size]` has `1 <= price, size <= 10^9`.
- Each `updates[j] = [price, size]` has `1 <= price <= 10^9` and `0 <= size <= 10^9`.
### Examples
```text
Input: snapshot = [[100, 5], [101, 10], [102, 3]], updates = [[101, 0], [103, 7], [100, 8]]
Output: [[100, 8], [102, 3], [103, 7]]
```
The book starts as `{100: 5, 101: 10, 102: 3}`. The first update removes 101, the second inserts 103 with size 7, and the third replaces the size at 100 with 8.
```text
Input: snapshot = [[50, 1]], updates = [[50, 0]]
Output: []
```
The only level, at price 50, is removed, which leaves an empty book.
```text
Input: snapshot = [[200, 4], [150, 2]], updates = [[175, 0], [120, 6], [150, 0], [150, 9], [120, 3]]
Output: [[120, 3], [150, 9], [200, 4]]
```
Removing 175 does nothing because that price was never in the book. Price 150 is removed and then inserted again with size 9, price 120 is inserted with size 6 and then replaced with 3, and 200 keeps its snapshot size.
Overview: Apply a stream of delta updates to an order book snapshot, where a size of zero removes a price level and a positive size inserts or replaces it, then return the final book sorted by price. The problem tests careful handling of update semantics, edge cases such as removing a missing level, and time complexity at 100,000 updates.