Merge multiple sorted arrays using min-heap
Company: Microsoft
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates algorithm design and data structure proficiency, focusing on merging multiple sorted sequences and reasoning about time and space complexity for large numbers of arrays and elements.
Constraints
- 1 <= k <= 10^5
- 0 <= N <= 10^6 total elements across all arrays
- Each individual array length can be zero or more
- Integer values fit in 32-bit signed range
- Input arrays may be empty and the merged result may be empty
Examples
Input: ([[1, 4, 9], [2, 6], [0, 3, 7, 8]],)
Expected Output: [0, 1, 2, 3, 4, 6, 7, 8, 9]
Explanation: The example case: three sorted arrays merged into one fully sorted list.
Input: ([],)
Expected Output: []
Explanation: No arrays at all -> empty result.
Input: ([[]],)
Expected Output: []
Explanation: A single empty array -> empty result; the heap is never seeded.
Input: ([[5]],)
Expected Output: [5]
Explanation: One array with one element -> returned as-is.
Input: ([[1, 1, 1], [1, 1]],)
Expected Output: [1, 1, 1, 1, 1]
Explanation: All-duplicate values across arrays; ties are handled by the array/element index in the heap tuple.
Input: ([[-5, -1, 3], [-3, 0, 2], []],)
Expected Output: [-5, -3, -1, 0, 2, 3]
Explanation: Negative values plus an empty array interleaved correctly.
Input: ([[1, 2, 3], [], [4, 5, 6]],)
Expected Output: [1, 2, 3, 4, 5, 6]
Explanation: An empty array in the middle is skipped; the two non-empty arrays concatenate in order.
Hints
- Use a min-heap (priority queue) that holds the current smallest unconsumed element of each non-empty array.
- Store tuples of (value, array_index, element_index) so that after popping the minimum you know which array to advance.
- Initialize the heap with the first element of every non-empty array, then loop: pop the minimum, append it, and push the next element from that same array until the heap is empty.
- This yields O(N log k) — strictly better than the O(N*k) repeated-linear-scan approach when k is large.