0
0
DSA C++programming~20 mins

Merge K Sorted Lists Using Min Heap in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Merge K Sorted Lists Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of merging 3 sorted lists using min heap
What is the printed merged list after running the following C++ code that merges 3 sorted linked lists using a min heap?
DSA C++
#include <iostream>
#include <vector>
#include <queue>

struct ListNode {
    int val;
    ListNode* next;
    ListNode(int x) : val(x), next(nullptr) {}
};

struct compare {
    bool operator()(ListNode* a, ListNode* b) {
        return a->val > b->val;
    }
};

ListNode* mergeKLists(std::vector<ListNode*>& lists) {
    std::priority_queue<ListNode*, std::vector<ListNode*>, compare> minHeap;
    for (auto node : lists) {
        if (node) minHeap.push(node);
    }
    ListNode dummy(0);
    ListNode* tail = &dummy;
    while (!minHeap.empty()) {
        ListNode* top = minHeap.top();
        minHeap.pop();
        tail->next = top;
        tail = tail->next;
        if (top->next) minHeap.push(top->next);
    }
    return dummy.next;
}

int main() {
    ListNode a1(1), a2(4), a3(5);
    a1.next = &a2; a2.next = &a3;
    ListNode b1(1), b2(3), b3(4);
    b1.next = &b2; b2.next = &b3;
    ListNode c1(2), c2(6);
    c1.next = &c2;
    std::vector<ListNode*> lists = {&a1, &b1, &c1};
    ListNode* merged = mergeKLists(lists);
    while (merged) {
        std::cout << merged->val << " -> ";
        merged = merged->next;
    }
    std::cout << "null" << std::endl;
    return 0;
}
A1 -> 1 -> 2 -> 3 -> 4 -> 4 -> 5 -> 6 -> null
B1 -> 2 -> 3 -> 4 -> 4 -> 5 -> 6 -> null
C1 -> 1 -> 3 -> 4 -> 5 -> 6 -> null
D1 -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null
Attempts:
2 left
💡 Hint
Trace the min heap operations and how nodes are popped and pushed.
🧠 Conceptual
intermediate
1:30remaining
Time complexity of merging K sorted lists using min heap
What is the time complexity of merging K sorted lists with a total of N elements using a min heap?
AO(K log N)
BO(N log K)
CO(N K)
DO(N log N)
Attempts:
2 left
💡 Hint
Consider how many times elements are pushed and popped from the heap and the heap size.
🔧 Debug
advanced
2:00remaining
Identify the runtime error in min heap merge code
What runtime error will occur when running this code snippet that merges K sorted lists using a min heap?
DSA C++
#include <queue>
#include <vector>

struct ListNode {
    int val;
    ListNode* next;
    ListNode(int x) : val(x), next(nullptr) {}
};

struct compare {
    bool operator()(ListNode* a, ListNode* b) {
        return a->val < b->val; // Note: reversed comparison
    }
};

ListNode* mergeKLists(std::vector<ListNode*>& lists) {
    std::priority_queue<ListNode*, std::vector<ListNode*>, compare> minHeap;
    for (auto node : lists) {
        if (node) minHeap.push(node);
    }
    ListNode dummy(0);
    ListNode* tail = &dummy;
    while (!minHeap.empty()) {
        ListNode* top = minHeap.top();
        minHeap.pop();
        tail->next = top;
        tail = tail->next;
        if (top->next) minHeap.push(top->next);
    }
    return dummy.next;
}
AThe merged list will be sorted in descending order, not ascending
BCompilation error due to wrong comparator syntax
CInfinite loop because minHeap never empties
DSegmentation fault due to null pointer dereference
Attempts:
2 left
💡 Hint
Check the comparator logic and how it affects the priority queue ordering.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in min heap comparator
Which option contains the correct syntax for a comparator struct to use in a C++ priority_queue for merging K sorted lists?
Astruct compare { bool operator()(ListNode a, ListNode b) { return a.val > b.val; } };
Bstruct compare { bool operator(ListNode* a, ListNode* b) { return a->val > b->val; } };
Cstruct compare { bool operator()(ListNode* a, ListNode* b) { return a->val > b->val; } };
Dstruct compare { bool operator[](ListNode* a, ListNode* b) { return a->val > b->val; } };
Attempts:
2 left
💡 Hint
Look for the correct operator overloading syntax for function call operator.
🚀 Application
expert
1:00remaining
Number of nodes in merged list after merging K sorted lists
Given 4 sorted linked lists with lengths 3, 5, 2, and 4 respectively, how many nodes will the merged linked list contain after merging all using a min heap?
A9
B12
C10
D14
Attempts:
2 left
💡 Hint
Sum the lengths of all input lists.