Quick Overview

Merge k sorted integer arrays, each standing in for an iterator, into one sorted list that keeps every duplicate. It tests k-way merging, reasoning about cost in terms of total elements and number of inputs, and edge cases such as empty iterators.

Merge k sorted iterators into a single sorted list

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given `k` sorted iterators, each represented as an array of integers in non-decreasing order. Implement a function that merges all `k` iterators into a single sorted stream and returns the result as a list. This simulates a union iterator (also known as a merge iterator) that supports `next()` and `has_next()`. `next()` returns the next smallest element across all iterators, and `has_next()` checks whether any elements remain. Calling `next()` until `has_next()` is false yields exactly the list your function returns. ### Function Signature ```python def merge_k_sorted_iterators(iterators: list[list[int]]) -> list[int]: ``` ### Rules - Each inner list is one iterator, already sorted in non-decreasing order. An inner list may be empty. - Keep every occurrence. A value that appears several times in one iterator, or in several iterators, appears that many times in the output. - The output contains all elements from all iterators in non-decreasing order. If every iterator is empty, return an empty list. ### Constraints - `1 <= k <= 500` - The total number of elements across all iterators is between 0 and `10^5`. - `-10^9 <= element value <= 10^9` - Each individual iterator is sorted in non-decreasing order. ### Examples ```text Input: iterators = [[1, 4, 7], [2, 5, 8], [3, 6, 9]] Output: [1, 2, 3, 4, 5, 6, 7, 8, 9] ``` The three iterators interleave evenly, so the merge takes one element from each in turn. ```text Input: iterators = [[1, 3, 5, 7], [2, 4, 6], [0, 8, 9, 10]] Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] ``` The iterators have different lengths. The 0 at the head of the third iterator is the smallest value overall, so it comes first, and the third iterator supplies the last three values after the other two run out. ```text Input: iterators = [[1, 1, 3], [], [1, 2]] Output: [1, 1, 1, 2, 3] ``` The empty iterator contributes nothing. The value 1 appears twice in the first iterator and once in the third, and all three copies are kept.

Overview: Merge k sorted integer arrays, each standing in for an iterator, into one sorted list that keeps every duplicate. It tests k-way merging, reasoning about cost in terms of total elements and number of inputs, and edge cases such as empty iterators.

You are given `k` sorted iterators. Each iterator is represented as a list of integers in non-decreasing order, and an iterator may be empty. Merge all `k` iterators into a single sorted stream and return that stream as a list. This models a union iterator (also called a merge iterator) that supports `next()` and `has_next()`. `next()` returns the next smallest element across all iterators, and `has_next()` reports whether any element remains. Calling `next()` until `has_next()` is false yields exactly the list your function returns. You implement only the function `merge_k_sorted_iterators(iterators)`. Rules: - `iterators` holds `k` inner lists. Each inner list is one iterator, already sorted in non-decreasing order. An inner list may be empty. - Keep every occurrence. A value that appears several times in one iterator, or in several iterators, appears that many times in the output. - The output contains all elements from all iterators in non-decreasing order. If every iterator is empty, return an empty list. - Every element lies in [-10^9, 10^9], so every value fits in a signed 32-bit integer and none exceeds 2^31-1. Example 1: ```text Input: iterators = [[1, 4, 7], [2, 5, 8], [3, 6, 9]] Output: [1, 2, 3, 4, 5, 6, 7, 8, 9] ``` The three iterators interleave evenly, so the merge takes one element from each in turn. Example 2: ```text Input: iterators = [[1, 1, 3], [], [1, 2]] Output: [1, 1, 1, 2, 3] ``` The empty iterator contributes nothing. The value 1 appears twice in the first iterator and once in the third, and all three copies are kept.

Constraints

  • 1 <= k <= 500
  • The total number of elements across all iterators is between 0 and 10^5.
  • -10^9 <= element value <= 10^9
  • Each individual iterator is sorted in non-decreasing order.

Examples

Input: ([[1, 4, 7], [2, 5, 8], [3, 6, 9]],)

Expected Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]

Explanation: Source example 1: three iterators interleave evenly, so the merge takes one element from each in turn.

Input: ([[1, 3, 5, 7], [2, 4, 6], [0, 8, 9, 10]],)

Expected Output: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Explanation: Source example 2: iterators of different lengths; the third holds the overall minimum 0 and supplies 8, 9 and 10 after the others run out. Output length is exactly 11, value equal to index (0 to 10).

Hints

  1. The next value of the merged stream is always the smallest among the front elements of the iterators that still have elements left.
  2. Repeated values are never dropped: equal values from one iterator or from different iterators all appear in the output.
  3. Empty iterators contribute nothing, and an iterator that runs out stops supplying values while the others keep going.

Merge K Sorted Iterators into One Sorted List

mediumGeneralSoftware Engineer

You are given k sorted iterators. Each iterator is represented as a list of integers in non-decreasing order, and an iterator may be empty. Merge all k iterators into a single sorted stream and return that stream as a list.

This models a union iterator (also called a merge iterator) that supports next() and has_next(). next() returns the next smallest element across all iterators, and has_next() reports whether any element remains. Calling next() until has_next() is false yields exactly the list your function returns. You implement only the function merge_k_sorted_iterators(iterators).

Rules:

  • iterators holds k inner lists. Each inner list is one iterator, already sorted in non-decreasing order. An inner list may be empty.
  • Keep every occurrence. A value that appears several times in one iterator, or in several iterators, appears that many times in the output.
  • The output contains all elements from all iterators in non-decreasing order. If every iterator is empty, return an empty list.
  • Every element lies in [-10^9, 10^9], so every value fits in a signed 32-bit integer and none exceeds 2^31-1.

Example 1:

Input: iterators = [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9]

The three iterators interleave evenly, so the merge takes one element from each in turn.

Example 2:

Input: iterators = [[1, 1, 3], [], [1, 2]]
Output: [1, 1, 1, 2, 3]

The empty iterator contributes nothing. The value 1 appears twice in the first iterator and once in the third, and all three copies are kept.

Examples

Example 1
Input
([[1, 4, 7], [2, 5, 8], [3, 6, 9]],)
Output
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Notes
Source example 1: three iterators interleave evenly, so the merge takes one element from each in turn.
Example 2
Input
([[1, 3, 5, 7], [2, 4, 6], [0, 8, 9, 10]],)
Output
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Notes
Source example 2: iterators of different lengths; the third holds the overall minimum 0 and supplies 8, 9 and 10 after the others run out. Output length is exactly 11, value equal to index (0 to 10).

Constraints

  • •1 <= k <= 500
  • •The total number of elements across all iterators is between 0 and 10^5.
  • •-10^9 <= element value <= 10^9
  • •Each individual iterator is sorted in non-decreasing order.

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_k_sorted_iterators(iterators: list[list[int]]) -> list[int]:
Sample call
merge_k_sorted_iterators([[1, 4, 7], [2, 5, 8], [3, 6, 9]])
Language
Case 1
Input
([[1, 4, 7], [2, 5, 8], [3, 6, 9]],)
Expected
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Case 2
Input
([[1, 3, 5, 7], [2, 4, 6], [0, 8, 9, 10]],)
Expected
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Browse More Questions

Show the approach

Approach

Approach: merge the iterators in pairs, round by round. In each round the lists at positions 0 and 1 are merged with a two-pointer merge, then the lists at positions 2 and 3, and so on; when the count is odd, the last list carries over unchanged. Each round roughly halves the number of lists, so after about log2(k) rounds a single list remains, and that list is the answer.

Two-pointer merge: keep one index into each sorted list, repeatedly append the smaller front element (taking it from the first list when the fronts are equal) and advance that index. When one list runs out, append the rest of the other list.

Invariant and correctness: at the start of every round, each list is sorted in non-decreasing order and the lists together hold exactly the multiset of all input elements. Merging two sorted lists this way produces one sorted list that keeps every occurrence from both, so the invariant still holds after the round. When one list remains it is the whole multiset in non-decreasing order, which is exactly what calling next() until has_next() is false would produce. A sorted multiset of integers is unique, so the order in which equal values are taken from different iterators cannot change the result.

Edge cases: empty iterators merge to nothing, so if every iterator is empty the function returns an empty list. With k = 1 the single list is returned as is. All values lie in [-10^9, 10^9] and no arithmetic is done on them, so 32-bit integers suffice in every language.

A min-heap holding the current front element of each non-empty iterator, popping the smallest and pushing that iterator's next element, gives the same O(N log k) time.

Time complexity:
O(N log k + k), where N is the total number of elements
Space complexity:
O(N + k)