0
0
DSA C++programming~10 mins

Merge K Sorted Lists Using Min Heap in DSA C++ - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to push the first element of each list into the min heap.

DSA C++
for (int i = 0; i < lists.size(); i++) {
    if (lists[i] != nullptr) {
        minHeap.[1](lists[i]);
    }
}
Drag options to blanks, or click blank then click option'
Aempty
Bpop
Ctop
Dpush
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop instead of push to add elements.
Using top which only accesses the smallest element without adding.
2fill in blank
medium

Complete the code to get the smallest node from the min heap.

DSA C++
while (!minHeap.empty()) {
    ListNode* node = minHeap.[1]();
    minHeap.pop();
    // process node
}
Drag options to blanks, or click blank then click option'
Atop
Bpop
Cpush
Dempty
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() to get the element, which removes it immediately.
Using push() which adds elements instead of accessing.
3fill in blank
hard

Fix the error in the comparator struct for the min heap.

DSA C++
struct compare {
    bool operator() (ListNode* a, ListNode* b) {
        return a->val [1] b->val;
    }
};
Drag options to blanks, or click blank then click option'
A==
B>
C<
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' which creates a max heap instead of min heap.
Using '==' or '!=' which do not define order.
4fill in blank
hard

Fill both blanks to correctly add the next node to the min heap after popping the smallest node.

DSA C++
if (node->[1] != nullptr) {
    minHeap.[2](node->next);
}
Drag options to blanks, or click blank then click option'
Anext
Bprev
Cpush
Dpop
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' instead of 'next' to access the next node.
Using pop instead of push to add the node.
5fill in blank
hard

Fill all three blanks to correctly build the merged linked list.

DSA C++
ListNode dummy(0);
ListNode* tail = &dummy;

while (!minHeap.empty()) {
    ListNode* node = minHeap.[1]();
    minHeap.pop();
    tail->[2] = node;
    tail = tail->[3];

    if (node->next != nullptr) {
        minHeap.push(node->next);
    }
}
Drag options to blanks, or click blank then click option'
Atop
Bnext
Cval
Dprev
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() instead of top() to get the node.
Using 'val' or 'prev' instead of 'next' for linking.