0
0
DSA Javascriptprogramming~10 mins

Median of Data Stream Using Two Heaps in DSA Javascript - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Apeek
Bpop
Csort
Dpush
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.
2fill in blank
medium

Complete 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'
A1
B0
C2
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using 2 or more causes imbalance.
Using 0 means no difference allowed, which is too strict.
3fill in blank
hard

Fix 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'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Dividing before adding changes the result.
4fill in blank
hard

Fill 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'
A<
B>
Cpush
Dpop
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < reverses logic.
Using pop instead of push removes elements.
5fill in blank
hard

Fill 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'
A1
Bpop
C0
Dpush
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.