Quick Overview

Merge several sorted lists while preserving duplicates and empty inputs. Implement a heap-based merge, compare balanced pairwise merging, and explain running time, memory use, and linked-node adaptations.

Merge multiple sorted linked lists and compare merge strategies

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Onsite

Merge k sorted linked lists into one sorted sequence. For this practice interface, each linked list is supplied as an array of its values. Return the merged values in nondecreasing order, retaining duplicates. ### Function Signature ```python def merge_sorted_lists(lists: list[list[int]]) -> list[int]: ``` ### Rules Every input list is already sorted. Empty lists and an empty outer list are allowed. Explain a heap-based method and an alternative that merges lists in pairs. ### Constraints - `0 <= len(lists) <= 10000`. Total number of values is at most 100000. - Values are integers between -1000000000 and 1000000000, within signed 32-bit range. ### Examples ```text Input: lists = [[1, 4, 5], [1, 3, 4], [2, 6]] Output: [1, 1, 2, 3, 4, 4, 5, 6] ``` All input occurrences are retained. ```text Input: lists = [[], []] Output: [] ``` There are no values to merge.

Overview: Merge several sorted lists while preserving duplicates and empty inputs. Implement a heap-based merge, compare balanced pairwise merging, and explain running time, memory use, and linked-node adaptations.

Read the full Amazon Software Engineer interview experience this question came from

Merge k sorted linked lists into one sorted sequence. For this practice interface, each linked list is supplied as an array of its values. Return the merged values in nondecreasing order, retaining duplicates. ### Function Signature ```python def merge_sorted_lists(lists: list[list[int]]) -> list[int]: ``` ### Rules Every input list is already sorted. Empty lists and an empty outer list are allowed. Explain a heap-based method and an alternative that merges lists in pairs. ### Constraints - `0 <= len(lists) <= 10000`. Total number of values is at most 100000. - Values are integers between -1000000000 and 1000000000, within signed 32-bit range. ### Examples ```text Input: lists = [[1, 4, 5], [1, 3, 4], [2, 6]] Output: [1, 1, 2, 3, 4, 4, 5, 6] ``` All input occurrences are retained. ```text Input: lists = [[], []] Output: [] ``` There are no values to merge.

Constraints

  • `0 <= len(lists) <= 10000`. Total number of values is at most 100000.
  • Values are integers between -1000000000 and 1000000000, within signed 32-bit range.

Examples

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

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

Explanation: Interleave all lists while preserving duplicates.

Input: ([[], []],)

Expected Output: []

Explanation: Empty inner lists contribute nothing.

Hints

  1. The smallest current value must be at the start of one of the remaining lists.
  2. Merging two sorted lists preserves the sorted invariant.

Merge multiple sorted linked lists and compare merge strategies

hardAmazonSoftware Engineer

Merge k sorted linked lists into one sorted sequence. For this practice interface, each linked list is supplied as an array of its values. Return the merged values in nondecreasing order, retaining duplicates.

Function Signature

def merge_sorted_lists(lists: list[list[int]]) -> list[int]:

Rules

Every input list is already sorted. Empty lists and an empty outer list are allowed. Explain a heap-based method and an alternative that merges lists in pairs.

Constraints

  • 0 <= len(lists) <= 10000. Total number of values is at most 100000.
  • Values are integers between -1000000000 and 1000000000, within signed 32-bit range.

Examples

Input: lists = [[1, 4, 5], [1, 3, 4], [2, 6]]
Output: [1, 1, 2, 3, 4, 4, 5, 6]

All input occurrences are retained.

Input: lists = [[], []]
Output: []

There are no values to merge.

Examples

Example 1
Input
([[1, 4, 5], [1, 3, 4], [2, 6]],)
Output
[1, 1, 2, 3, 4, 4, 5, 6]
Notes
Interleave all lists while preserving duplicates.
Example 2
Input
([[], []],)
Output
[]
Notes
Empty inner lists contribute nothing.

Constraints

  • •`0 <= len(lists) <= 10000`. Total number of values is at most 100000.
  • •Values are integers between -1000000000 and 1000000000, within signed 32-bit range.

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_sorted_lists(lists: list[list[int]]) -> list[int]:
Sample call
merge_sorted_lists([[1, 4, 5], [1, 3, 4], [2, 6]])
Language
Case 1
Input
([[1, 4, 5], [1, 3, 4], [2, 6]],)
Expected
[1, 1, 2, 3, 4, 4, 5, 6]
Case 2
Input
([[], []],)
Expected
[]

Browse More Questions

Show the approach

Approach

The reference merges pairs of lists in rounds. To merge a pair, repeatedly take the smaller of the two unconsumed heads, then append the remainder of the longer list. This picks the smallest unconsumed value each time. Any unpaired list advances to the next round. Every round preserves all occurrences and halves the number of active lists, eventually producing the sorted sequence.

A heap-based approach instead inserts the first value of each nonempty list with its list and position. Repeatedly pop the smallest entry and insert the next value from its source, if present. This maintains one entry per active list and uses O(k) auxiliary space beyond the output. Both methods do O(k + N log(k + 1)) work in the worst case for k lists and N total values.

Time complexity:
O(k + N log(k + 1))
Space complexity:
O(k + N)