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 min heap
What is the output linked list after merging these 3 sorted lists using a min heap?
List 1: 1 -> 4 -> 5 -> null
List 2: 1 -> 3 -> 4 -> null
List 3: 2 -> 6 -> null
List 1: 1 -> 4 -> 5 -> null
List 2: 1 -> 3 -> 4 -> null
List 3: 2 -> 6 -> null
DSA C
/* Pseudocode for merging 3 sorted lists using min heap */ // Insert first node of each list into min heap // Extract min node, add to result, insert next node from same list // Repeat until heap empty // Final merged list output:
Attempts:
2 left
💡 Hint
Remember to include all nodes from all lists in sorted order.
✗ Incorrect
Merging 3 sorted lists using a min heap results in a fully sorted list containing all nodes from the input lists. The min heap always picks the smallest current node among the lists.
🧠 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 lists efficiently?
Attempts:
2 left
💡 Hint
Think about how to always pick the smallest next node efficiently.
✗ Incorrect
A min heap keeps track of the smallest node among the heads of all lists, allowing efficient extraction and insertion in O(log K) time, which reduces total comparisons.
🔧 Debug
advancedIdentify the error in this min heap merge code snippet
What error will this C code snippet cause when merging K sorted lists using a min heap?
```c
struct Node* mergeKLists(struct Node* lists[], int k) {
struct MinHeap* heap = createMinHeap(k);
for (int i = 0; i < k; i++) {
if (lists[i] != NULL) {
insertMinHeap(heap, lists[i]);
}
}
struct Node* head = NULL, *tail = NULL;
while (!isEmpty(heap)) {
struct Node* minNode = extractMin(heap);
if (head == NULL) {
head = tail = minNode;
} else {
tail->next = minNode;
tail = minNode;
}
if (minNode->next != NULL) {
insertMinHeap(heap, minNode->next);
}
}
return head;
}
```
Attempts:
2 left
💡 Hint
Check what happens to the next pointers of extracted nodes.
✗ Incorrect
Since the extracted minNode's next pointer is not set to NULL before linking, the merged list may contain cycles or incorrect links.
❓ Predict Output
advancedOutput after merging 4 sorted lists with duplicates
Given these 4 sorted lists, what is the output after merging using a min heap?
List 1: 2 -> 2 -> 5 -> null
List 2: 1 -> 3 -> 4 -> null
List 3: 2 -> 6 -> 7 -> null
List 4: 0 -> 8 -> null
List 1: 2 -> 2 -> 5 -> null
List 2: 1 -> 3 -> 4 -> null
List 3: 2 -> 6 -> 7 -> null
List 4: 0 -> 8 -> null
Attempts:
2 left
💡 Hint
Include all duplicates from all lists in sorted order.
✗ Incorrect
All nodes including duplicates are merged in sorted order using the min heap, so all three '2's appear in the final list.
🧠 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 nodes using a min heap?
Attempts:
2 left
💡 Hint
Consider how many times nodes are inserted and extracted from the heap and the heap size.
✗ Incorrect
Each of the N nodes is inserted and extracted once from a min heap of size at most K, so operations take O(log K) each, total O(N log K).
