0
0
DSA Typescriptprogramming~10 mins

Min Heap vs Max Heap When to Use Which in DSA Typescript - Interactive Comparison Practice

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

Complete the code to create a Min Heap by choosing the correct comparison operator.

DSA Typescript
class MinHeap {
  heap: number[] = [];

  insert(value: number) {
    this.heap.push(value);
    this.bubbleUp(this.heap.length - 1);
  }

  bubbleUp(index: number) {
    while (index > 0) {
      const parentIndex = Math.floor((index - 1) / 2);
      if (this.heap[index] [1] this.heap[parentIndex]) {
        [this.heap[index], this.heap[parentIndex]] = [this.heap[parentIndex], this.heap[index]];
        index = parentIndex;
      } else {
        break;
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
A>
B===
C<
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' causes a Max Heap behavior.
Using '===' does not maintain heap property.
2fill in blank
medium

Complete the code to create a Max Heap by choosing the correct comparison operator.

DSA Typescript
class MaxHeap {
  heap: number[] = [];

  insert(value: number) {
    this.heap.push(value);
    this.bubbleUp(this.heap.length - 1);
  }

  bubbleUp(index: number) {
    while (index > 0) {
      const parentIndex = Math.floor((index - 1) / 2);
      if (this.heap[index] [1] this.heap[parentIndex]) {
        [this.heap[index], this.heap[parentIndex]] = [this.heap[parentIndex], this.heap[index]];
        index = parentIndex;
      } else {
        break;
      }
    }
  }
}
Drag options to blanks, or click blank then click option'
A>
B<
C<=
D===
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causes a Min Heap behavior.
Using '===' does not maintain heap property.
3fill in blank
hard

Fix the error in the code that removes the root from a Min Heap by choosing the correct method to restore heap property.

DSA Typescript
class MinHeap {
  heap: number[] = [];

  removeRoot() {
    if (this.heap.length === 0) return null;
    const root = this.heap[0];
    this.heap[0] = this.heap.pop()!;
    this.[1](0);
    return root;
  }

  bubbleDown(index: number) {
    // Implementation omitted
  }
}
Drag options to blanks, or click blank then click option'
AbubbleDown
Bheapify
CbubbleUp
Dsort
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'bubbleUp' after removing root causes incorrect heap structure.
Using 'sort' is inefficient and not specific to heap.
4fill in blank
hard

Fill both blanks to complete the condition that decides when to swap nodes during bubbleDown in a Max Heap.

DSA Typescript
bubbleDown(index: number) {
  const left = 2 * index + 1;
  const right = 2 * index + 2;
  let largest = index;

  if (left < this.heap.length && this.heap[left] [1] this.heap[largest]) {
    largest = left;
  }

  if (right < this.heap.length && this.heap[right] [2] this.heap[largest]) {
    largest = right;
  }

  if (largest !== index) {
    [this.heap[index], this.heap[largest]] = [this.heap[largest], this.heap[index]];
    this.bubbleDown(largest);
  }
}
Drag options to blanks, or click blank then click option'
A>
B<
C>=
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' causes Min Heap behavior.
Using '>=' or '<=' may cause unnecessary swaps.
5fill in blank
hard

Fill all three blanks to create a function that returns the root element of a Min Heap without removing it.

DSA Typescript
peek(): number | null {
  if (this.heap.length === 0) {
    return [1];
  }
  return this.heap[[2]];
  // The root is at index [3]
}
Drag options to blanks, or click blank then click option'
Aundefined
B0
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Returning undefined instead of null for empty heap.
Using wrong index for root element.