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