Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop instead of push to add elements.
Using top which only accesses the smallest element without adding.
✗ Incorrect
We use push() to add elements to the min heap.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() to get the element, which removes it immediately.
Using push() which adds elements instead of accessing.
✗ Incorrect
top() returns the smallest element in the min heap without removing it.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' which creates a max heap instead of min heap.
Using '==' or '!=' which do not define order.
✗ Incorrect
For a min heap, the comparator returns true if a's value is greater than b's value.
4fill in blank
hardFill 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'
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.
✗ Incorrect
We check if node->next exists and then push it into the min heap.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() instead of top() to get the node.
Using 'val' or 'prev' instead of 'next' for linking.
✗ Incorrect
We get the smallest node with top(), link it by setting tail->next, and move tail to tail->next.