0
0
DSA Typescriptprogramming~20 mins

Min Heap vs Max Heap When to Use Which in DSA Typescript - 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 is best suited for using a Min Heap?
AImplementing a priority queue where highest priority is the largest value
BFinding the largest element quickly in a dynamic dataset
CSorting elements in descending order efficiently
DFinding the smallest element quickly in a dynamic dataset
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 for a Max Heap?
AFinding the smallest element quickly in a dynamic dataset
BSorting elements in ascending order efficiently
CFinding the largest element quickly in a dynamic dataset
DImplementing a priority queue where lowest priority is the largest value
Attempts:
2 left
💡 Hint
Max Heap keeps the largest element at the root.
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 Typescript
class MinHeap {
  heap: number[] = [];

  insert(val: number) {
    this.heap.push(val);
    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;
    }
  }

  printHeap() {
    return this.heap.join(' -> ') + ' -> null';
  }
}

const minHeap = new MinHeap();
[10, 4, 15, 20, 0].forEach(num => minHeap.insert(num));
console.log(minHeap.printHeap());
A0 -> 4 -> 15 -> 20 -> 10 -> null
B0 -> 4 -> 10 -> 20 -> 15 -> null
C0 -> 10 -> 4 -> 15 -> 20 -> null
D4 -> 0 -> 10 -> 15 -> 20 -> null
Attempts:
2 left
💡 Hint
Remember that in a Min Heap, the smallest element is at the root and parents are smaller than children.
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, 8, 5, 12, 7?
DSA Typescript
class MaxHeap {
  heap: number[] = [];

  insert(val: number) {
    this.heap.push(val);
    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;
    }
  }

  printHeap() {
    return this.heap.join(' -> ') + ' -> null';
  }
}

const maxHeap = new MaxHeap();
[3, 8, 5, 12, 7].forEach(num => maxHeap.insert(num));
console.log(maxHeap.printHeap());
A12 -> 7 -> 5 -> 3 -> 8 -> null
B12 -> 8 -> 5 -> 3 -> 7 -> null
C8 -> 7 -> 5 -> 3 -> 12 -> null
D7 -> 12 -> 5 -> 3 -> 8 -> null
Attempts:
2 left
💡 Hint
Max Heap keeps the largest element at the root and parents larger than children.
🚀 Application
expert
3:00remaining
Choosing Heap Type for Median Maintenance
You want to maintain the median of a stream of numbers efficiently. Which heap combination is best to use?
AOne Min Heap for the larger half and one Max Heap for the smaller half
BTwo Min Heaps, one for the smaller half and one for the larger half
CTwo Max Heaps, one for the smaller half and one for the larger half
DOne Max Heap for the larger half and one Min Heap for the smaller half
Attempts:
2 left
💡 Hint
Think about how to quickly access the largest of the smaller half and the smallest of the larger half.