0
0
DSA Javascriptprogramming~20 mins

Min Heap vs Max Heap When to Use Which in DSA Javascript - Compare & Choose

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Heap Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
When to use a Min Heap?

Which scenario best fits using a Min Heap?

ASorting elements in descending order efficiently
BFinding the smallest element quickly in a large dataset
CFinding the largest element quickly in a large dataset
DStoring elements without any order
Attempts:
2 left
💡 Hint

Think about which heap type keeps the smallest element at the top.

🧠 Conceptual
intermediate
2:00remaining
When to use a Max Heap?

Which use case is best suited for a Max Heap?

AFinding the smallest element quickly
BStoring elements in random order
CImplementing a queue with FIFO order
DFinding the largest element quickly
Attempts:
2 left
💡 Hint

Consider which heap type keeps the largest element at the top.

Predict Output
advanced
2:00remaining
Output of Min Heap after insertions

What is the printed state of the Min Heap after inserting these numbers in order: 10, 4, 15, 20, 0?

DSA Javascript
class MinHeap {
  constructor() {
    this.heap = [];
  }
  insert(num) {
    this.heap.push(num);
    this.bubbleUp();
  }
  bubbleUp() {
    let index = this.heap.length - 1;
    while (index > 0) {
      let parentIndex = Math.floor((index - 1) / 2);
      if (this.heap[parentIndex] <= this.heap[index]) break;
      [this.heap[parentIndex], this.heap[index]] = [this.heap[index], this.heap[parentIndex]];
      index = parentIndex;
    }
  }
  print() {
    return this.heap.join(' -> ') + ' -> null';
  }
}

const heap = new MinHeap();
[10, 4, 15, 20, 0].forEach(n => heap.insert(n));
console.log(heap.print());
A0 -> 4 -> 15 -> 20 -> 10 -> null
B0 -> 4 -> 10 -> 20 -> 15 -> null
C4 -> 0 -> 10 -> 15 -> 20 -> null
D10 -> 4 -> 15 -> 20 -> 0 -> null
Attempts:
2 left
💡 Hint

Remember that in a Min Heap, the smallest element is always at the root and the heap property must be maintained after each insertion.

Predict Output
advanced
2:00remaining
Output of Max Heap after insertions

What is the printed state of the Max Heap after inserting these numbers in order: 3, 9, 5, 1, 12?

DSA Javascript
class MaxHeap {
  constructor() {
    this.heap = [];
  }
  insert(num) {
    this.heap.push(num);
    this.bubbleUp();
  }
  bubbleUp() {
    let index = this.heap.length - 1;
    while (index > 0) {
      let parentIndex = Math.floor((index - 1) / 2);
      if (this.heap[parentIndex] >= this.heap[index]) break;
      [this.heap[parentIndex], this.heap[index]] = [this.heap[index], this.heap[parentIndex]];
      index = parentIndex;
    }
  }
  print() {
    return this.heap.join(' -> ') + ' -> null';
  }
}

const heap = new MaxHeap();
[3, 9, 5, 1, 12].forEach(n => heap.insert(n));
console.log(heap.print());
A12 -> 9 -> 5 -> 1 -> 3 -> null
B12 -> 9 -> 3 -> 1 -> 5 -> null
C9 -> 12 -> 5 -> 3 -> 1 -> null
D3 -> 9 -> 5 -> 1 -> 12 -> null
Attempts:
2 left
💡 Hint

Remember that in a Max Heap, the largest element is always at the root and the heap property must be maintained after each insertion.

🚀 Application
expert
3:00remaining
Choosing Heap Type for Real-Time Median Calculation

You want to calculate the median of a stream of numbers in real-time. Which combination of heaps is best to maintain the median efficiently?

AUse one Min Heap to store all numbers
BUse two Min Heaps to store all numbers
CUse one Max Heap for the lower half and one Min Heap for the upper half of numbers
DUse one Max Heap to store all numbers
Attempts:
2 left
💡 Hint

Think about splitting the data into two halves to quickly find the middle value.