Merge Two Sorted Key-Value Streams, Summing Values for Shared Keys

Quick Overview

Merge two key-sorted lists of key-value pairs into one sorted list, adding the values when a key appears in both, without building a hash map. Tests the two-pointer merge step from merge sort and careful handling of shared keys and leftover entries.

Merge Two Sorted Key-Value Streams, Summing Values for Shared Keys

Company: Databricks

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given two sorted lists of `[key, value]` pairs, `stream1` and `stream2`. Both lists are sorted by key in ascending order, and each key is unique within its own list. Merge the two lists into a single sorted list of `[key, value]` pairs. If the same key appears in both lists, combine them into a single entry whose value is the sum of the two values. The result must be sorted by key in ascending order. Simulate a streaming merge: do not load the entire dataset into a hash map. Use a technique like the merge step of merge sort instead. ### Function Signature ```python def merge_sorted_streams(stream1: list[list[int]], stream2: list[list[int]]) -> list[list[int]]: ``` ### Rules - Each entry is a two-element list `[key, value]`, and each output entry has the same shape. - A key that appears in only one list is copied to the output with its value unchanged. - A key that appears in both lists produces exactly one output entry. That entry is kept even when the summed value is 0. - Either list may be empty. If both are empty, return `[]`. ### Constraints - `0 <= len(stream1), len(stream2) <= 10^5` - Keys are integers sorted in ascending order within each stream. - Values are integers in the range `[-10^9, 10^9]`. - Each key appears at most once per input list. ### Examples ```text Input: stream1 = [[1,10],[3,20],[5,30]], stream2 = [[2,15],[3,25],[6,35]] Output: [[1,10],[2,15],[3,45],[5,30],[6,35]] ``` Key 3 appears in both streams with values 20 and 25, so its entry has value 45. Every other key appears in only one stream and keeps its value. ```text Input: stream1 = [[1,5],[2,10],[4,15]], stream2 = [[1,3],[2,7],[4,9]] Output: [[1,8],[2,17],[4,24]] ``` Every key appears in both streams: 5 + 3 = 8, 10 + 7 = 17, and 15 + 9 = 24. ```text Input: stream1 = [[-3,5],[0,1]], stream2 = [[-3,-5],[8,2]] Output: [[-3,0],[0,1],[8,2]] ``` Key -3 sums to 0 and is still kept. After key 0 the first stream is exhausted, so the remaining entry `[8,2]` from the second stream is appended.

Overview: Merge two key-sorted lists of key-value pairs into one sorted list, adding the values when a key appears in both, without building a hash map. Tests the two-pointer merge step from merge sort and careful handling of shared keys and leftover entries.

Databricks logo
Databricks
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given two sorted lists of [key, value] pairs, stream1 and stream2. Both lists are sorted by key in ascending order, and each key is unique within its own list.

Merge the two lists into a single sorted list of [key, value] pairs. If the same key appears in both lists, combine them into a single entry whose value is the sum of the two values.

The result must be sorted by key in ascending order. Simulate a streaming merge: do not load the entire dataset into a hash map. Use a technique like the merge step of merge sort instead.

Function Signature

def merge_sorted_streams(stream1: list[list[int]], stream2: list[list[int]]) -> list[list[int]]:

Rules

  • Each entry is a two-element list [key, value] , and each output entry has the same shape.
  • A key that appears in only one list is copied to the output with its value unchanged.
  • A key that appears in both lists produces exactly one output entry. That entry is kept even when the summed value is 0.
  • Either list may be empty. If both are empty, return [] .

Constraints

  • 0 <= len(stream1), len(stream2) <= 10^5
  • Keys are integers sorted in ascending order within each stream.
  • Values are integers in the range [-10^9, 10^9] .
  • Each key appears at most once per input list.

Examples

Input: stream1 = [[1,10],[3,20],[5,30]], stream2 = [[2,15],[3,25],[6,35]]
Output: [[1,10],[2,15],[3,45],[5,30],[6,35]]

Key 3 appears in both streams with values 20 and 25, so its entry has value 45. Every other key appears in only one stream and keeps its value.

Input: stream1 = [[1,5],[2,10],[4,15]], stream2 = [[1,3],[2,7],[4,9]]
Output: [[1,8],[2,17],[4,24]]

Every key appears in both streams: 5 + 3 = 8, 10 + 7 = 17, and 15 + 9 = 24.

Input: stream1 = [[-3,5],[0,1]], stream2 = [[-3,-5],[8,2]]
Output: [[-3,0],[0,1],[8,2]]

Key -3 sums to 0 and is still kept. After key 0 the first stream is exhausted, so the remaining entry [8,2] from the second stream is appended.