Merge k Sorted Lists
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon reported this one in September 2026, and the whole question hinges on one data structure: a min-heap. Merge k sorted linked lists sounds like a linked-list problem, but the real work is deciding which of k heads goes next. If you've got an Amazon OA coming, expect this or a close cousin. It's a classic for a reason. The trick is small, the edge cases are nastier than the algorithm, and blanking on either costs you. StealthCoder sits invisibly on your screen as a safety net if your mind goes empty mid-assessment.
The problem
You are given an array lists containing k linked-list heads. Every linked list is sorted in ascending order. Merge all of the linked lists into one ascending linked list and return its head. Function mergeKLists(lists: ListNode[]) → ListNode Examples Example 1 lists = [[1,4,5],[1,3,4],[2,6]] return = [1,1,2,3,4,4,5,6] The linked lists are: 1 -> 4 -> 5 1 -> 3 -> 4 2 -> 6 Merging them produces the sorted linked list 1 -> 1 -> 2 -> 3 -> 4 -> 4 -> 5 -> 6. Example 2 lists = [] return = [] Example 3 lists = [[]] return = [] Constraints k == lists.length 0 <= k <= 10^4 0 <= lists[i].length <= 500 -10^4 <= lists[i][j] <= 10^4 Each lists[i] is sorted in ascending order. The sum of all lists[i].length values does not exceed 10^4.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The pattern is a min-heap of list heads. Push the first node of every non-empty list, pop the smallest, append it to your result, then push that node's next. Each pop and push costs O(log k), so total time is O(N log k) where N is the total node count. The alternative is divide and conquer: merge lists in pairs, round after round, same complexity and no heap. The pitfalls are mundane. Empty input, lists = [[]] with null heads, and tie-breaking in languages where the heap compares nodes directly and crashes on equal values. Store (value, index, node) tuples or define a comparator. Use a dummy head node so you don't special-case the first append. Don't merge one list at a time into an accumulator, that drifts toward O(N*k). If you freeze on the heap setup during the live OA, StealthCoder is the hedge that gets you a working skeleton fast.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Merge k Sorted Lists cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as merge k sorted lists. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Merge k Sorted Lists FAQ
What's the trick to Merge k Sorted Lists?+
Keep a min-heap holding the current head of each list. Pop the smallest, attach it to your output, then push its next node. The heap never holds more than k items, so each step is O(log k). A dummy head node keeps the output building clean.
How hard is this really for an Amazon OA?+
It's a LeetCode hard by label but medium in practice once you know the heap approach. The logic is short. Most failures come from null handling, empty lists, and heap comparison errors on equal values, not from the algorithm itself.
Heap or divide and conquer, which should I write?+
Either gets O(N log k). The heap is more intuitive and quicker to code if your language has a priority queue built in. Divide and conquer avoids comparator headaches and reuses a standard two-list merge. Pick whichever you can write without looking anything up.
What edge cases break most solutions?+
An empty lists array, a lists array containing only empty lists, and duplicate values that make the heap compare nodes directly. Filter out null heads before pushing, and add an index as a tiebreaker so comparisons never fall through to the node objects.
How do I prepare for this in 48 hours?+
Write the heap version once from scratch, then the pairwise merge version once. Test both on the three examples, especially the empty ones. Know the complexity cold: O(N log k) time, O(k) extra space for the heap. That's enough for this problem.