Merge k sorted iterators into a single sorted list

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.

General logo
General
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

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

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

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.

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.

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.