Quick 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.

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.

You are building a real-time order book feed for an exchange. The book starts from an initial `snapshot`, a list of `[price, size]` pairs giving the resting liquidity at each price level. You then receive a list of `updates`. Each update `[price, size]` is a delta applied to the book, and the updates are applied in the order given: - If `price` is not in the book and `size > 0`, insert a new level `[price, size]`. - If `price` is already in the book and `size > 0`, replace that level's size with `size`. The old size is discarded, so sizes never accumulate. - If `size == 0`, remove the level at `price` if it is present. A size-0 update for a price that is not in the book changes nothing. When several updates touch the same price, the last one decides its final state. A price can be removed and inserted again later. Every level and every update is a two-element list `[price, size]`. Each price appears at most once in `snapshot`, and the snapshot is not necessarily sorted. Update prices may repeat and may or may not be in the book. Implement `apply_updates(snapshot, updates)`. Return the final book as a list of `[price, size]` lists sorted by `price` in ascending order. Prices in the book are distinct, so this order is unique. If the book ends up empty, return `[]`. Every price and size is at most 10^9, which fits in a signed 32-bit integer. Example 1: ```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. Example 2: ```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.

Constraints

  • 1 <= len(snapshot) <= 10^5
  • 1 <= len(updates) <= 10^5
  • Each snapshot[i] = [price, size] has 1 <= price <= 10^9 and 1 <= size <= 10^9
  • Each updates[j] = [price, size] has 1 <= price <= 10^9 and 0 <= size <= 10^9
  • Each price appears at most once in snapshot; update prices may repeat and may or may not be in the book

Examples

Input: ([[100, 5], [101, 10], [102, 3]], [[101, 0], [103, 7], [100, 8]])

Expected Output: [[100, 8], [102, 3], [103, 7]]

Explanation: Source example 1: 101 is removed, 103 is inserted with size 7, and 100 is replaced with size 8.

Input: ([[200, 4], [150, 2]], [[175, 0], [120, 6], [150, 0], [150, 9], [120, 3]])

Expected Output: [[120, 3], [150, 9], [200, 4]]

Explanation: Source example 3: removing absent 175 does nothing, 150 is removed then reinserted with 9, and 120 is inserted with 6 then replaced with 3.

Hints

  1. Trace the three update rules on a single price first: a positive size always sets the level, and a zero size always clears it, whatever came before.
  2. The snapshot can arrive in any order, and inserted prices can land below, above or between existing ones, so the final ordering by price has to be produced explicitly.
  3. A zero-size update for a price that is not in the book must not leave a level behind.

Apply Price-Level Updates to an Order Book Snapshot

easyOptiverSoftware Engineer

You are building a real-time order book feed for an exchange. The book starts from an initial snapshot, a list of [price, size] pairs giving the resting liquidity at each price level. You then receive a list of updates. Each update [price, size] is a delta applied to the book, and the updates are applied in the order given:

  • If price is not in the book and size > 0, insert a new level [price, size].
  • If price is already in the book and size > 0, replace that level's size with size. The old size is discarded, so sizes never accumulate.
  • If size == 0, remove the level at price if it is present. A size-0 update for a price that is not in the book changes nothing.

When several updates touch the same price, the last one decides its final state. A price can be removed and inserted again later.

Every level and every update is a two-element list [price, size]. Each price appears at most once in snapshot, and the snapshot is not necessarily sorted. Update prices may repeat and may or may not be in the book.

Implement apply_updates(snapshot, updates). Return the final book as a list of [price, size] lists sorted by price in ascending order. Prices in the book are distinct, so this order is unique. If the book ends up empty, return [].

Every price and size is at most 10^9, which fits in a signed 32-bit integer.

Example 1:

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.

Example 2:

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.

Examples

Example 1
Input
([[100, 5], [101, 10], [102, 3]], [[101, 0], [103, 7], [100, 8]])
Output
[[100, 8], [102, 3], [103, 7]]
Notes
Source example 1: 101 is removed, 103 is inserted with size 7, and 100 is replaced with size 8.
Example 2
Input
([[200, 4], [150, 2]], [[175, 0], [120, 6], [150, 0], [150, 9], [120, 3]])
Output
[[120, 3], [150, 9], [200, 4]]
Notes
Source example 3: removing absent 175 does nothing, 150 is removed then reinserted with 9, and 120 is inserted with 6 then replaced with 3.

Constraints

  • •1 <= len(snapshot) <= 10^5
  • •1 <= len(updates) <= 10^5
  • •Each snapshot[i] = [price, size] has 1 <= price <= 10^9 and 1 <= size <= 10^9
  • •Each updates[j] = [price, size] has 1 <= price <= 10^9 and 0 <= size <= 10^9
  • •Each price appears at most once in snapshot; update prices may repeat and may or may not be in the book

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def apply_updates(snapshot: list[list[int]], updates: list[list[int]]) -> list[list[int]]:
Sample call
apply_updates([[100, 5], [101, 10], [102, 3]], [[101, 0], [103, 7], [100, 8]])
Language
Case 1
Input
([[100, 5], [101, 10], [102, 3]], [[101, 0], [103, 7], [100, 8]])
Expected
[[100, 8], [102, 3], [103, 7]]
Case 2
Input
([[200, 4], [150, 2]], [[175, 0], [120, 6], [150, 0], [150, 9], [120, 3]])
Expected
[[120, 3], [150, 9], [200, 4]]

Browse More Questions

Show the approach

Approach

Approach: keep the book in a hash map from price to size. Load the snapshot, then walk the updates in order. A positive size runs book[price] = size, which covers both the insert rule and the replace rule. A zero size deletes price from the map if it is there and does nothing otherwise. When the updates run out, sort the remaining prices and emit one [price, size] list per price.

Invariant: after the first j updates, the map holds exactly the book after those j updates, with one entry per live price. It holds before any update because snapshot prices are distinct, and each update changes only its own price, exactly as its rule says. So the map at the end is the final book, and since its prices are distinct, sorting by price gives the one valid output order.

The same rules mean a price's final state depends only on the last update that touched it, or on its snapshot entry when no update did. The JavaScript and Java references use that view to stay within flat numeric arrays: they number every snapshot entry and update by its position in the combined sequence, sort by the single 64-bit value price * total + position (at most about 2 * 10^14), and in each run of equal prices keep the last entry if its size is positive. The C++ reference keeps a std::map, which stays ordered by price as updates arrive.

Edge cases: a size-0 update for an absent price never creates a [price, 0] level; a price can be removed and inserted again; the book can drain to empty partway through and refill; an empty final book returns []. Prices and sizes are at most 10^9 and nothing is summed, so 32-bit integers hold every input and output value.

Time complexity:
O((n + m) log(n + m)), where n = len(snapshot) and m = len(updates)
Space complexity:
O(n + m)