Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to push elements into the max heap.
DSA C++
std::priority_queue<int> maxHeap; for (int num : nums) { maxHeap.[1](num); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() instead of push() to add elements.
Trying to access top() to add elements.
Checking empty() instead of adding elements.
✗ Incorrect
We use push() to add elements to the max heap.
2fill in blank
mediumComplete the code to remove the largest element from the max heap.
DSA C++
if (!maxHeap.empty()) { maxHeap.[1](); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using push() instead of pop() to remove elements.
Trying to access top() to remove elements.
Calling empty() which only checks if heap is empty.
✗ Incorrect
pop() removes the largest element from the max heap.
3fill in blank
hardFix the error in accessing the largest element from the max heap.
DSA C++
int largest = maxHeap.[1](); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() which removes the element instead of accessing it.
Using push() which adds elements.
Using empty() which returns a boolean.
✗ Incorrect
top() returns the largest element without removing it.
4fill in blank
hardFill both blanks to extract the kth largest element using the max heap.
DSA C++
for (int i = 1; i < [1]; ++i) { maxHeap.[2](); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using push() instead of pop() inside the loop.
Looping up to size instead of k.
Using wrong variable names.
✗ Incorrect
We pop the largest element k-1 times to reach the kth largest.
5fill in blank
hardFill all three blanks to complete the function that returns the kth largest element.
DSA C++
int findKthLargest(std::vector<int>& nums, int [1]) { std::priority_queue<int> maxHeap; for (int num : nums) { maxHeap.[2](num); } for (int i = 1; i < [3]; ++i) { maxHeap.pop(); } return maxHeap.top(); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using size instead of k for the loop.
Using pop() instead of push() to add elements.
Incorrect parameter name.
✗ Incorrect
Parameter k is used to control the loop and push adds elements to the heap.