Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add a number to the max heap.
DSA Typescript
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 use peek which only views the top element.
✗ 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 size is greater than min heap size by more than one.
DSA Typescript
minHeap.[1](maxHeap.[2]());
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using peek instead of pop to remove element.
Using shift which is not a heap method.
✗ Incorrect
We pop the top from maxHeap and push it into minHeap to balance sizes.
3fill in blank
hardFix the error in the code to get the median when heaps are of equal size.
DSA Typescript
return (maxHeap.[1]() + minHeap.[1]()) / 2;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() removes elements and changes the heap.
Using push() is for adding elements, not retrieving.
✗ Incorrect
We use peek() to get the top elements without removing them.
4fill in blank
hardFill both blanks to correctly insert a number into the appropriate heap.
DSA Typescript
if (num [1] maxHeap.[2]()) { maxHeap.push(num); } else { minHeap.push(num); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop() instead of peek() to compare.
Using > instead of <= changes the logic.
✗ Incorrect
We compare num with the top of maxHeap using peek() and insert accordingly.
5fill in blank
hardFill all three blanks to rebalance heaps when min heap size is greater than max heap size.
DSA Typescript
if (minHeap.size() > maxHeap.size() + [1]) { maxHeap.[2](minHeap.[3]()); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 2 instead of 1 for size difference check.
Using pop and push in wrong order.
✗ Incorrect
We check if minHeap is bigger by 1, then push the popped element from minHeap to maxHeap.