0
0
DSA Javascriptprogramming~10 mins

Median of Data Stream Using Two Heaps in DSA Javascript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Median of Data Stream Using Two Heaps
New number arrives
Compare with maxHeap top?
Add to maxHeap
Balance heaps size?
If size difference > 1
Move root from bigger heap to smaller heap
Calculate median
Return median
New numbers go into one of two heaps (maxHeap for smaller half, minHeap for larger half). We keep heaps balanced, then median is top(s) of heaps.
Execution Sample
DSA Javascript
class MedianFinder {
  constructor() {
    this.maxHeap = new MaxHeap();
    this.minHeap = new MinHeap();
  }
  addNum(num) {
    // add number logic
  }
  findMedian() {
    // median logic
  }
}
This class adds numbers to two heaps and finds median efficiently.
Execution Table
StepOperationNumber AddedMaxHeap NodesMinHeap NodesBalance ActionMedian CalculationVisual State
1Add number5[5][]No balance neededMedian = 5MaxHeap: [5], MinHeap: []
2Add number2[5, 2][]Balance: Move 5 to MinHeapMedian = (2 + 5)/2 = 3.5MaxHeap: [2], MinHeap: [5]
3Add number10[2][5, 10]Balance: Move 5 to MaxHeapMedian = 5MaxHeap: [5, 2], MinHeap: [10]
4Add number1[5, 2, 1][10]No balance neededMedian = (2 + 5)/2 = 3.5MaxHeap: [5, 2, 1], MinHeap: [10]
5Add number3[5, 3, 1, 2][10]Balance: Move 5 to MinHeapMedian = 3MaxHeap: [3, 2, 1], MinHeap: [5, 10]
6Add number8[3, 2, 1][5, 10, 8]Balance: Move 5 to MaxHeapMedian = 5MaxHeap: [5, 3, 1, 2], MinHeap: [8, 10]
7Add number7[5, 3, 1, 2][7, 8, 10]Balance: Move 7 to MaxHeapMedian = (5 + 7)/2 = 6MaxHeap: [7, 5, 1, 2, 3], MinHeap: [8, 10]
8Add number6[7, 6, 1, 2, 3, 5][8, 10]No balance neededMedian = 6MaxHeap: [7, 6, 1, 2, 3, 5], MinHeap: [8, 10]
9Add number4[7, 6, 4, 2, 3, 5, 1][8, 10]Balance: Move 7 to MinHeapMedian = (6 + 7)/2 = 6.5MaxHeap: [6, 5, 4, 2, 3, 1], MinHeap: [7, 8, 10]
10Add number9[6, 5, 4, 2, 3, 1][7, 8, 10, 9]Balance: Move 7 to MaxHeapMedian = 7MaxHeap: [7, 6, 4, 2, 3, 1, 5], MinHeap: [8, 9, 10]
11Stop-[7, 6, 4, 2, 3, 1, 5][8, 9, 10]Heaps balancedMedian = 7Final state
💡 All numbers added and heaps balanced; median calculated at each step.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7After 8After 9After 10Final
maxHeap[][5][2][2][5,2,1][3,2,1][3,2,1][7,5,1,2,3][7,6,1,2,3,5][6,5,4,2,3,1][7,6,4,2,3,1,5][7,6,4,2,3,1,5]
minHeap[][][5][5,10][10][10][8,10][8,10][7,8,10][8,9,10][8,9,10][8,9,10]
medianN/A53.553.535666.577
Key Moments - 3 Insights
Why do we move elements between heaps after adding a number?
Because heaps must be balanced in size (difference at most 1) to correctly find the median. See execution_table rows 2, 3, 5, 6 where balance actions move elements.
Why do we add smaller numbers to maxHeap and larger to minHeap?
MaxHeap stores the smaller half so its top is the largest of the smaller numbers; MinHeap stores larger half so its top is smallest of larger numbers. This split helps find median quickly. See concept_flow and execution_table steps.
How is median calculated when heaps have equal size vs different sizes?
If heaps are equal size, median is average of both tops (rows 2,5,9). If one heap is bigger, median is top of that heap (rows 1,3,4,6,8,10).
Visual Quiz - 3 Questions
Test your understanding
Look at execution_table row 5, what is the median after adding number 3?
A4
B3
C5
D3.5
💡 Hint
Check the 'Median Calculation' column in row 5 of execution_table.
At which step does the maxHeap first contain 7 elements?
AStep 9
BStep 10
CStep 7
DStep 11
💡 Hint
Look at 'MaxHeap Nodes' column in execution_table rows for size changes.
If we add a number smaller than all existing numbers, which heap will it go to initially?
AminHeap
BEither heap randomly
CmaxHeap
DNeither heap
💡 Hint
Refer to concept_flow: smaller numbers go to maxHeap.
Concept Snapshot
Median of Data Stream Using Two Heaps:
- Use maxHeap for smaller half, minHeap for larger half
- Add new number to correct heap based on comparison
- Balance heaps sizes (difference ≤ 1)
- Median is top of bigger heap or average of both tops
- Efficient for streaming data median calculation
Full Transcript
This concept uses two heaps to find the median of a stream of numbers efficiently. New numbers are added to either a maxHeap (holding smaller half) or a minHeap (holding larger half). After each addition, heaps are balanced so their sizes differ by at most one. The median is then either the top of the bigger heap or the average of both tops if sizes are equal. The execution table shows step-by-step how numbers are added, heaps balanced, and median calculated. The variable tracker records heap contents and median after each step. Key moments clarify why balancing is needed, how numbers are assigned to heaps, and how median is computed. The visual quiz tests understanding of median values and heap sizes at specific steps.