Merge multiple sorted linked lists and compare merge strategies
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
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
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
- The smallest current value must be at the start of one of the remaining lists.
- Merging two sorted lists preserves the sorted invariant.