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 output of the merged linked list after running this code?
DSA Python
import heapq class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def merge_k_lists(lists): min_heap = [] for i, node in enumerate(lists): if node: heapq.heappush(min_heap, (node.val, i, node)) dummy = ListNode(0) current = dummy while min_heap: val, i, node = heapq.heappop(min_heap) current.next = ListNode(val) current = current.next if node.next: heapq.heappush(min_heap, (node.next.val, i, node.next)) return dummy.next # Lists: # 1 -> 4 -> 5 # 1 -> 3 -> 4 # 2 -> 6 l1 = ListNode(1, ListNode(4, ListNode(5))) l2 = ListNode(1, ListNode(3, ListNode(4))) l3 = ListNode(2, ListNode(6)) merged = merge_k_lists([l1, l2, l3]) # Print merged list result = [] while merged: result.append(merged.val) merged = merged.next print(result)
Attempts:
2 left
💡 Hint
Think about how the min heap always picks the smallest current node from all lists.
✗ Incorrect
The min heap merges nodes by always choosing the smallest value among the heads of the lists. The merged list contains all elements in sorted order.
🧠 Conceptual
intermediateWhy use a min heap to merge K sorted lists?
What is the main advantage of using a min heap to merge K sorted linked lists?
Attempts:
2 left
💡 Hint
Think about how to efficiently find the smallest element among multiple sorted lists.
✗ Incorrect
A min heap keeps track of the smallest elements from each list, allowing efficient extraction and insertion in O(log K) time, where K is the number of lists.
❓ Predict Output
advancedOutput of merging empty and non-empty lists
What is the output of merging these lists using the min heap approach?
DSA Python
import heapq class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def merge_k_lists(lists): min_heap = [] for i, node in enumerate(lists): if node: heapq.heappush(min_heap, (node.val, i, node)) dummy = ListNode(0) current = dummy while min_heap: val, i, node = heapq.heappop(min_heap) current.next = ListNode(val) current = current.next if node.next: heapq.heappush(min_heap, (node.next.val, i, node.next)) return dummy.next l1 = None l2 = ListNode(0) l3 = None merged = merge_k_lists([l1, l2, l3]) result = [] while merged: result.append(merged.val) merged = merged.next print(result)
Attempts:
2 left
💡 Hint
Consider how the function handles empty lists and single-node lists.
✗ Incorrect
Only the second list has one node with value 0. The merged list contains just that value.
🔧 Debug
advancedIdentify the error in this min heap merge implementation
What error will this code raise when merging K sorted lists using a min heap?
DSA Python
import heapq class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def merge_k_lists(lists): min_heap = [] for node in lists: if node: heapq.heappush(min_heap, (node.val, node)) dummy = ListNode(0) current = dummy while min_heap: val, node = heapq.heappop(min_heap) current.next = ListNode(val) current = current.next if node.next: heapq.heappush(min_heap, (node.next.val, node.next)) return dummy.next l1 = ListNode(1, ListNode(3)) l2 = ListNode(1) merged = merge_k_lists([l1, l2])
Attempts:
2 left
💡 Hint
Consider what happens when two nodes have the same value in the heap.
✗ Incorrect
When two nodes have the same value, the heap tries to compare ListNode objects which is not supported, causing a TypeError.
🚀 Application
expertTime complexity of merging K sorted lists with N total elements using a min heap
What is the time complexity of merging K sorted linked 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 heap and the heap size.
✗ Incorrect
Each of the N elements is pushed and popped once from a heap of size at most K, so the complexity is O(N log K).
