Challenge - 5 Problems
Merge K Sorted Lists Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of merging 3 sorted lists using a min heap
What is the printed merged list after running the following Java code that merges 3 sorted linked lists using a min heap?
DSA Java
import java.util.*; class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; } } public class MergeKLists { public static ListNode mergeKLists(ListNode[] lists) { PriorityQueue<ListNode> minHeap = new PriorityQueue<>(Comparator.comparingInt(a -> a.val)); for (ListNode node : lists) { if (node != null) minHeap.offer(node); } ListNode dummy = new ListNode(0); ListNode current = dummy; while (!minHeap.isEmpty()) { ListNode smallest = minHeap.poll(); current.next = smallest; current = current.next; if (smallest.next != null) minHeap.offer(smallest.next); } return dummy.next; } public static void printList(ListNode head) { while (head != null) { System.out.print(head.val + " -> "); head = head.next; } System.out.println("null"); } public static void main(String[] args) { ListNode l1 = new ListNode(1); l1.next = new ListNode(4); l1.next.next = new ListNode(5); ListNode l2 = new ListNode(1); l2.next = new ListNode(3); l2.next.next = new ListNode(4); ListNode l3 = new ListNode(2); l3.next = new ListNode(6); ListNode[] lists = new ListNode[]{l1, l2, l3}; ListNode merged = mergeKLists(lists); printList(merged); } }
Attempts:
2 left
💡 Hint
Trace the smallest elements picked from each list step-by-step using the min heap.
✗ Incorrect
The min heap always picks the smallest current node among all lists. The merged list contains all elements in sorted order: 1,1,2,3,4,4,5,6.
🧠 Conceptual
intermediateUnderstanding the role of the min heap in merging K sorted lists
Why is a min heap used when merging K sorted linked lists?
Attempts:
2 left
💡 Hint
Think about how to efficiently find the smallest element among multiple sorted lists at each step.
✗ Incorrect
A min heap allows efficient extraction of the smallest element among the heads of all lists, enabling the merged list to be built in sorted order without sorting all elements at once.
🔧 Debug
advancedIdentify the runtime error in the mergeKLists method
What runtime error will occur when running the following code snippet for merging K sorted lists using a min heap?
DSA Java
PriorityQueue<ListNode> minHeap = new PriorityQueue<>(); for (ListNode node : lists) { if (node != null) minHeap.offer(node); } while (!minHeap.isEmpty()) { ListNode smallest = minHeap.poll(); current.next = smallest; current = current.next; if (smallest.next != null) minHeap.offer(smallest.next); }
Attempts:
2 left
💡 Hint
Check if ListNode has a natural ordering or if a comparator is provided to PriorityQueue.
✗ Incorrect
PriorityQueue without a comparator requires elements to implement Comparable. ListNode does not implement Comparable, so ClassCastException occurs at runtime.
❓ Predict Output
advancedOutput after merging empty and non-empty lists
What is the output of the merged list after running this code with one empty list and two non-empty sorted lists?
DSA Java
ListNode l1 = null; ListNode l2 = new ListNode(2); l2.next = new ListNode(3); ListNode l3 = new ListNode(1); l3.next = new ListNode(4); ListNode[] lists = new ListNode[]{l1, l2, l3}; ListNode merged = mergeKLists(lists); printList(merged);
Attempts:
2 left
💡 Hint
Empty lists do not affect the merged output; the merged list is sorted from the non-empty lists.
✗ Incorrect
The merged list contains all elements from the non-empty lists in sorted order: 1,2,3,4.
🧠 Conceptual
expertTime complexity of merging K sorted lists using a min heap
What is the time complexity of merging K sorted lists with a total of N elements using a min heap?
Attempts:
2 left
💡 Hint
Consider how many times elements are pushed and popped from the min heap and the heap size.
✗ Incorrect
Each of the N elements is pushed and popped from a min heap of size at most K, so each operation costs O(log K), resulting in O(N log K) total time.
