0
0
DSA Typescriptprogramming~20 mins

Build Heap from Array Heapify in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Heap Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output array after building a max heap?
Given the array [3, 9, 2, 1, 4, 5], what is the array representation after applying the build max heap process?
DSA Typescript
function buildMaxHeap(arr: number[]): number[] {
  const n = arr.length;
  for (let i = Math.floor(n / 2) - 1; i >= 0; i--) {
    heapify(arr, n, i);
  }
  return arr;
}

function heapify(arr: number[], n: number, i: number): void {
  let largest = i;
  const left = 2 * i + 1;
  const right = 2 * i + 2;

  if (left < n && arr[left] > arr[largest]) {
    largest = left;
  }

  if (right < n && arr[right] > arr[largest]) {
    largest = right;
  }

  if (largest !== i) {
    [arr[i], arr[largest]] = [arr[largest], arr[i]];
    heapify(arr, n, largest);
  }
}

const input = [3, 9, 2, 1, 4, 5];
console.log(buildMaxHeap(input));
A[9, 3, 5, 1, 4, 2]
B[5, 4, 9, 1, 3, 2]
C[9, 4, 5, 1, 3, 2]
D[9, 4, 2, 1, 3, 5]
Attempts:
2 left
💡 Hint
Start heapifying from the last non-leaf node and move upwards.
Predict Output
intermediate
2:00remaining
What is the output after building a min heap?
Given the array [7, 2, 6, 3, 9, 1], what is the array representation after applying the build min heap process?
DSA Typescript
function buildMinHeap(arr: number[]): number[] {
  const n = arr.length;
  for (let i = Math.floor(n / 2) - 1; i >= 0; i--) {
    minHeapify(arr, n, i);
  }
  return arr;
}

function minHeapify(arr: number[], n: number, i: number): void {
  let smallest = i;
  const left = 2 * i + 1;
  const right = 2 * i + 2;

  if (left < n && arr[left] < arr[smallest]) {
    smallest = left;
  }

  if (right < n && arr[right] < arr[smallest]) {
    smallest = right;
  }

  if (smallest !== i) {
    [arr[i], arr[smallest]] = [arr[smallest], arr[i]];
    minHeapify(arr, n, smallest);
  }
}

const input = [7, 2, 6, 3, 9, 1];
console.log(buildMinHeap(input));
A[1, 2, 7, 3, 9, 6]
B[2, 3, 1, 7, 9, 6]
C[1, 2, 6, 3, 9, 7]
D[1, 3, 2, 7, 9, 6]
Attempts:
2 left
💡 Hint
Heapify from the last non-leaf node upwards to maintain min heap property.
🔧 Debug
advanced
2:00remaining
Identify the error in this heapify implementation
What error will this code produce when building a max heap from array [4, 10, 3, 5, 1]?
DSA Typescript
function heapify(arr: number[], n: number, i: number): void {
  let largest = i;
  const left = 2 * i + 1;
  const right = 2 * i + 2;

  if (left < n && arr[left] > arr[largest]) {
    largest = left;
  }

  if (right < n && arr[right] > arr[largest]) {
    largest = right;
  }

  if (largest !== i) {
    [arr[i], arr[largest]] = [arr[largest], arr[i]];
    heapify(arr, n, largest);
  }
}

function buildMaxHeap(arr: number[]): number[] {
  const n = arr.length;
  for (let i = Math.floor(n / 2) - 1; i >= 0; i--) {
    heapify(arr, n, i);
  }
  return arr;
}

const input = [4, 10, 3, 5, 1];
console.log(buildMaxHeap(input));
ANo error, outputs [10, 5, 3, 4, 1]
BTypeError: Cannot read property '5' of undefined
CIndexError: list index out of range
DSyntaxError: Unexpected token
Attempts:
2 left
💡 Hint
Check the boundary conditions for left and right child indices.
🧠 Conceptual
advanced
2:00remaining
Why does build heap run in O(n) time, not O(n log n)?
Which explanation best describes why the build heap operation using heapify runs in linear time O(n) instead of O(n log n)?
ABecause the number of nodes at each level decreases exponentially while heapify cost increases linearly, balancing to O(n).
BBecause heapify is called only on leaf nodes which take constant time each.
CBecause the array is already sorted before building the heap.
DBecause heapify uses a divide and conquer approach that halves the problem size each time.
Attempts:
2 left
💡 Hint
Consider how many nodes are at each level and how much work heapify does per node.
🚀 Application
expert
3:00remaining
After building max heap, what is the heap array after extracting max once?
Given the array [4, 10, 3, 5, 1], after building a max heap and then extracting the max element once (removing root and heapifying), what is the resulting array?
DSA Typescript
function heapify(arr: number[], n: number, i: number): void {
  let largest = i;
  const left = 2 * i + 1;
  const right = 2 * i + 2;

  if (left < n && arr[left] > arr[largest]) {
    largest = left;
  }

  if (right < n && arr[right] > arr[largest]) {
    largest = right;
  }

  if (largest !== i) {
    [arr[i], arr[largest]] = [arr[largest], arr[i]];
    heapify(arr, n, largest);
  }
}

function buildMaxHeap(arr: number[]): void {
  const n = arr.length;
  for (let i = Math.floor(n / 2) - 1; i >= 0; i--) {
    heapify(arr, n, i);
  }
}

function extractMax(arr: number[]): number | null {
  const n = arr.length;
  if (n === 0) return null;
  const max = arr[0];
  arr[0] = arr[n - 1];
  arr.pop();
  heapify(arr, arr.length, 0);
  return max;
}

const input = [4, 10, 3, 5, 1];
buildMaxHeap(input);
extractMax(input);
console.log(input);
A[10, 5, 3, 1]
B[5, 1, 3, 4]
C[4, 5, 3, 1]
D[5, 4, 3, 1]
Attempts:
2 left
💡 Hint
After removing max, replace root with last element and heapify down.