Challenge - 5 Problems
Min Heap Merge Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of merging 3 sorted linked lists using min heap
What is the printed state of the merged linked list after running the following C# code that merges 3 sorted linked lists using a min heap?
DSA C#
using System; using System.Collections.Generic; public class ListNode { public int val; public ListNode next; public ListNode(int val=0, ListNode next=null) { this.val = val; this.next = next; } } public class Solution { public ListNode MergeKLists(ListNode[] lists) { var minHeap = new SortedSet<(int val, int idx, ListNode node)>(Comparer<(int, int, ListNode)>.Create((a, b) => { int cmp = a.val.CompareTo(b.val); if (cmp == 0) return a.idx.CompareTo(b.idx); return cmp; })); for (int i = 0; i < lists.Length; i++) { if (lists[i] != null) { minHeap.Add((lists[i].val, i, lists[i])); } } ListNode dummy = new ListNode(); ListNode current = dummy; while (minHeap.Count > 0) { var smallest = minHeap.Min; minHeap.Remove(smallest); current.next = smallest.node; current = current.next; if (smallest.node.next != null) { minHeap.Add((smallest.node.next.val, smallest.idx, smallest.node.next)); } } return dummy.next; } public static void PrintList(ListNode head) { while (head != null) { Console.Write(head.val); if (head.next != null) Console.Write(" -> "); head = head.next; } Console.WriteLine(" -> null"); } public static void Main() { ListNode l1 = new ListNode(1, new ListNode(4, new ListNode(5))); ListNode l2 = new ListNode(1, new ListNode(3, new ListNode(4))); ListNode l3 = new ListNode(2, new ListNode(6)); Solution sol = new Solution(); ListNode merged = sol.MergeKLists(new ListNode[] { l1, l2, l3 }); PrintList(merged); } }
Attempts:
2 left
💡 Hint
Think about how the min heap always picks the smallest current node from all lists.
✗ Incorrect
The min heap picks the smallest node among the heads of all lists repeatedly. The merged list contains all nodes in sorted order from all three lists combined.
🧠 Conceptual
intermediateWhy use a min heap to merge K sorted lists?
Which of the following best explains why a min heap is used to merge K sorted linked lists efficiently?
Attempts:
2 left
💡 Hint
Think about how to always pick the smallest element efficiently.
✗ Incorrect
A min heap keeps track of the smallest elements from each list, allowing efficient extraction of the smallest node at each step, which leads to an overall time complexity of O(N log K) where N is total nodes and K is number of lists.
🔧 Debug
advancedIdentify the error in this min heap merge implementation
What error will the following code produce when merging K sorted lists using a min heap in C#?
DSA C#
var minHeap = new SortedSet<(int val, ListNode node)>(Comparer<(int, ListNode)>.Create((a, b) => a.val.CompareTo(b.val))); for (int i = 0; i < lists.Length; i++) { if (lists[i] != null) { minHeap.Add((lists[i].val, lists[i])); } } while (minHeap.Count > 0) { var smallest = minHeap.Min; minHeap.Remove(smallest); current.next = smallest.node; current = current.next; if (smallest.node.next != null) { minHeap.Add((smallest.node.next.val, smallest.node.next)); } }
Attempts:
2 left
💡 Hint
Consider how SortedSet compares tuples with same values but different nodes.
✗ Incorrect
SortedSet requires unique keys. When two nodes have the same value, the comparer returns 0, causing SortedSet to treat them as duplicates and throw an exception.
❓ Predict Output
advancedOutput after merging empty and non-empty lists
What is the output of merging the following 3 lists using min heap? Lists: [] (empty), [2 -> 3 -> 7], [1 -> 4]
DSA C#
ListNode l1 = null; ListNode l2 = new ListNode(2, new ListNode(3, new ListNode(7))); ListNode l3 = new ListNode(1, new ListNode(4)); Solution sol = new Solution(); ListNode merged = sol.MergeKLists(new ListNode[] { l1, l2, l3 }); Solution.PrintList(merged);
Attempts:
2 left
💡 Hint
Empty lists do not affect the merged output.
✗ Incorrect
The merged list contains all nodes from the non-empty lists in sorted order: 1, 2, 3, 4, 7.
🧠 Conceptual
expertTime complexity of merging K sorted lists using a min heap
What is the time complexity of merging K sorted linked lists with a total of N nodes using a min heap?
Attempts:
2 left
💡 Hint
Consider how many times you insert and remove nodes from the heap and the heap size.
✗ Incorrect
Each of the N nodes is inserted and removed from the min heap once. The heap size is at most K, so each operation takes O(log K). Total time is O(N log K).
