Quick Overview

Merge sorted streams of timestamped price changes into one cumulative price series. Combine simultaneous changes before output, preserve zero-net-change timestamps, and handle empty streams and negative values.

Merge timestamped price changes into a cumulative price series

Company: Citadel

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Several streams contain records `[timestamp, change]`, each sorted by nondecreasing timestamp. A record changes the current price by its signed change value. Merge the streams and return the price after all changes at each distinct timestamp have been applied. ### Function Signature ```python def merge_price_changes(streams: list[list[list[int]]], initial_price: int) -> list[list[int]]: ``` ### Rules - `initial_price` is the price before every record. This parameter makes the starting-price convention explicit. - Return `[timestamp, resulting_price]` pairs in strictly increasing timestamp order. - Combine all records with the same timestamp, including duplicates within one stream, before emitting a result. - Emit every timestamp present in the input, even when its net change is zero. Empty input streams contribute nothing. ### Constraints These bounds define this practice version. - There are between 0 and 10000 streams and at most 100000 records in total. - `0 <= timestamp <= 1000000000`. - Changes and `initial_price` are between -1000000000 and 1000000000. - Resulting prices may exceed signed 32-bit range but have absolute value at most 100001000000000, below the exact-integer limit of `2 ** 53`. ### Examples ```text Input: streams = [[[1, 5], [3, -2]], [[1, -1], [2, 4]], []], initial_price = 10 Output: [[1, 14], [2, 18], [3, 16]] ``` At timestamp 1, changes 5 and -1 apply together before the first output. ```text Input: streams = [[[2, 3], [2, -3]]], initial_price = -4 Output: [[2, -4]] ``` A zero net change still produces a record for timestamp 2.

Overview: Merge sorted streams of timestamped price changes into one cumulative price series. Combine simultaneous changes before output, preserve zero-net-change timestamps, and handle empty streams and negative values.

Several streams contain records `[timestamp, change]`, each sorted by nondecreasing timestamp. A record changes the current price by its signed change value. Merge the streams and return the price after all changes at each distinct timestamp have been applied. ### Function Signature ```python def merge_price_changes(streams: list[list[list[int]]], initial_price: int) -> list[list[int]]: ``` ### Rules - `initial_price` is the price before every record. This parameter makes the starting-price convention explicit. - Return `[timestamp, resulting_price]` pairs in strictly increasing timestamp order. - Combine all records with the same timestamp, including duplicates within one stream, before emitting a result. - Emit every timestamp present in the input, even when its net change is zero. Empty input streams contribute nothing. ### Constraints These bounds define this practice version. - There are between 0 and 10000 streams and at most 100000 records in total. - `0 <= timestamp <= 1000000000`. - Changes and `initial_price` are between -1000000000 and 1000000000. - Resulting prices may exceed signed 32-bit range but have absolute value at most 100001000000000, below the exact-integer limit of `2 ** 53`. ### Examples ```text Input: streams = [[[1, 5], [3, -2]], [[1, -1], [2, 4]], []], initial_price = 10 Output: [[1, 14], [2, 18], [3, 16]] ``` At timestamp 1, changes 5 and -1 apply together before the first output. ```text Input: streams = [[[2, 3], [2, -3]]], initial_price = -4 Output: [[2, -4]] ``` A zero net change still produces a record for timestamp 2.

Constraints

  • There are between 0 and 10000 streams and at most 100000 records in total.
  • `0 <= timestamp <= 1000000000`.
  • Changes and `initial_price` are between -1000000000 and 1000000000.
  • Resulting prices may exceed signed 32-bit range but have absolute value at most 100001000000000, below the exact-integer limit of `2 ** 53`.

Examples

Input: ([[[1, 5], [3, -2]], [[1, -1], [2, 4]], []], 10)

Expected Output: [[1, 14], [2, 18], [3, 16]]

Explanation: All changes at timestamp one apply before emitting its price.

Input: ([[[2, 3], [2, -3]]], -4)

Expected Output: [[2, -4]]

Explanation: A same-stream cancellation still emits its timestamp.

Hints

  1. At any step, the next unseen record from each nonempty stream has its stream's lowest remaining timestamp.
  2. Advance every record at the current timestamp before emitting its price.

Merge timestamped price changes into a cumulative price series

mediumCitadelSoftware Engineer

Several streams contain records [timestamp, change], each sorted by nondecreasing timestamp. A record changes the current price by its signed change value. Merge the streams and return the price after all changes at each distinct timestamp have been applied.

Function Signature

def merge_price_changes(streams: list[list[list[int]]], initial_price: int) -> list[list[int]]:

Rules

  • initial_price is the price before every record. This parameter makes the starting-price convention explicit.
  • Return [timestamp, resulting_price] pairs in strictly increasing timestamp order.
  • Combine all records with the same timestamp, including duplicates within one stream, before emitting a result.
  • Emit every timestamp present in the input, even when its net change is zero. Empty input streams contribute nothing.

Constraints

These bounds define this practice version.

  • There are between 0 and 10000 streams and at most 100000 records in total.
  • 0 <= timestamp <= 1000000000.
  • Changes and initial_price are between -1000000000 and 1000000000.
  • Resulting prices may exceed signed 32-bit range but have absolute value at most 100001000000000, below the exact-integer limit of 2 ** 53.

Examples

Input: streams = [[[1, 5], [3, -2]], [[1, -1], [2, 4]], []], initial_price = 10
Output: [[1, 14], [2, 18], [3, 16]]

At timestamp 1, changes 5 and -1 apply together before the first output.

Input: streams = [[[2, 3], [2, -3]]], initial_price = -4
Output: [[2, -4]]

A zero net change still produces a record for timestamp 2.

Examples

Example 1
Input
([[[1, 5], [3, -2]], [[1, -1], [2, 4]], []], 10)
Output
[[1, 14], [2, 18], [3, 16]]
Notes
All changes at timestamp one apply before emitting its price.
Example 2
Input
([[[2, 3], [2, -3]]], -4)
Output
[[2, -4]]
Notes
A same-stream cancellation still emits its timestamp.

Constraints

  • •There are between 0 and 10000 streams and at most 100000 records in total.
  • •`0 <= timestamp <= 1000000000`.
  • •Changes and `initial_price` are between -1000000000 and 1000000000.
  • •Resulting prices may exceed signed 32-bit range but have absolute value at most 100001000000000, below the exact-integer limit of `2 ** 53`.

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 merge_price_changes(streams: list[list[list[int]]], initial_price: int) -> list[list[int]]:
Sample call
merge_price_changes([[[1, 5], [3, -2]], [[1, -1], [2, 4]], []], 10)
Language
Case 1
Input
([[[1, 5], [3, -2]], [[1, -1], [2, 4]], []], 10)
Expected
[[1, 14], [2, 18], [3, 16]]
Case 2
Input
([[[2, 3], [2, -3]]], -4)
Expected
[[2, -4]]

Browse More Questions

Show the approach

Approach

Track one current record from each nonempty stream in a min-heap keyed by timestamp. The root is the earliest unconsumed timestamp because each stream is sorted. Record that timestamp, pop and apply a change, and push the next record from that same stream. Keep processing the root while its timestamp matches, including newly exposed records from the same stream. Only then emit the current running price.

Every input record is inserted and removed once, and after each group the next heap timestamp is strictly greater. Therefore each timestamp is emitted exactly once, with all earlier and equal changes applied. Use an exact 64-bit accumulator in Java and C++; JavaScript totals remain within its exact integer range.

Time complexity:
O(k + T log(k + 1)) for k streams and T records
Space complexity:
O(k) auxiliary space, plus the output