Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to insert a number into the max heap.
DSA Javascript
maxHeap.[1](num); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop instead of push removes elements instead of adding.
Using peek only looks at the top element, does not add.
✗ Incorrect
We use push to add a new number to the max heap.
2fill in blank
mediumComplete the code to balance the heaps when max heap has more elements.
DSA Javascript
if (maxHeap.size() > minHeap.size() + [1]) { minHeap.push(maxHeap.pop()); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 2 or more causes imbalance.
Using 0 means no difference allowed, which is too strict.
✗ Incorrect
We balance heaps when max heap size is more than min heap size by 1.
3fill in blank
hardFix the error in the code to get the median when heaps are equal size.
DSA Javascript
return (maxHeap.peek() [1] minHeap.peek()) / 2;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Dividing before adding changes the result.
✗ Incorrect
Median is average of top elements, so we add then divide by 2.
4fill in blank
hardFill both blanks to correctly decide which heap to push the new number into.
DSA Javascript
if (num [1] maxHeap.peek()) { maxHeap.push(num); } else { minHeap.[2](num); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < reverses logic.
Using pop instead of push removes elements.
✗ Incorrect
If the number is less than max heap top, push to max heap; else push to min heap.
5fill in blank
hardFill all three blanks to correctly rebalance heaps and return the median.
DSA Javascript
if (maxHeap.size() > minHeap.size() + [1]) { minHeap.push(maxHeap.[2]()); } else if (minHeap.size() > maxHeap.size() + [3]) { maxHeap.push(minHeap.pop()); } if (maxHeap.size() === minHeap.size()) { return (maxHeap.peek() + minHeap.peek()) / 2; } else { return maxHeap.peek(); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 as size difference allows no difference, causing unnecessary moves.
Using push instead of pop to remove element causes errors.
✗ Incorrect
We rebalance when size difference is more than 1 by moving top element using pop, then return median.