Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a max heap by negating values for use with JavaScript's min heap.
DSA Javascript
const maxHeap = arr.map(num => num [1] -1);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '*' to negate values.
Using '-' which is subtraction, not negation.
✗ Incorrect
Multiplying each number by -1 converts the array into negative values, allowing a min heap to behave like a max heap.
2fill in blank
mediumComplete the code to pop the largest element from the max heap (min heap of negatives).
DSA Javascript
const largest = -maxHeap.[1](); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'push' which adds elements instead of removing.
Using 'shift' which removes from front but not heap-specific.
✗ Incorrect
Using 'pop' removes the smallest element from the min heap, which corresponds to the largest original value after negation.
3fill in blank
hardFix the error in the code to build a max heap using JavaScript's priority queue simulation.
DSA Javascript
const maxHeap = new MinPriorityQueue({ priority: x => [1] }); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'x' which keeps min heap behavior.
Using 'Math.abs(x)' which does not invert order.
✗ Incorrect
Using '-x' as priority inverts the order, making the min priority queue behave like a max heap.
4fill in blank
hardFill both blanks to extract the kth largest element using a max heap.
DSA Javascript
for(let i = 0; i < k - 1; i++) { heap.[1](); } return heap.[2]();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'push' which adds elements instead of removing.
Using 'peek' which only views top element without removing.
✗ Incorrect
We remove the top element k-1 times using 'extractMax', then return the kth largest by extracting max again.
5fill in blank
hardFill all three blanks to build a max heap and find the kth largest element.
DSA Javascript
const maxHeap = new MinPriorityQueue({ priority: x => [1] });
arr.forEach(num => maxHeap.[2](num));
for(let i = 0; i < k - 1; i++) maxHeap.[3]();
return maxHeap.dequeue().element; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'push' which is not a method of MinPriorityQueue.
Not negating priority, so min heap behavior remains.
✗ Incorrect
Negate values for max heap simulation, add elements with 'enqueue', remove top elements with 'dequeue' until kth largest.