Challenge - 5 Problems
Heap Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Min-Heap after buildHeap
What is the printed state of the min-heap array after running the buildHeap function on the input array?
DSA Javascript
function buildMinHeap(arr) {
const n = arr.length;
function heapify(i) {
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]];
heapify(smallest);
}
}
for (let i = Math.floor(n / 2) - 1; i >= 0; i--) {
heapify(i);
}
return arr;
}
const input = [9, 4, 7, 1, -2, 6, 5];
const heap = buildMinHeap(input);
console.log(heap);Attempts:
2 left
💡 Hint
Remember that buildHeap starts heapifying from the last parent node up to the root.
✗ Incorrect
The buildMinHeap function heapifies from the last parent node to the root, ensuring the smallest element is at the root. After heapify, the array represents a valid min-heap.
❓ Predict Output
intermediate2:00remaining
Max-Heap Array after Heapify
What is the state of the array after building a max-heap from the given input array?
DSA Javascript
function buildMaxHeap(arr) {
const n = arr.length;
function heapify(i) {
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(largest);
}
}
for (let i = Math.floor(n / 2) - 1; i >= 0; i--) {
heapify(i);
}
return arr;
}
const input = [3, 9, 2, 1, 4, 5];
const heap = buildMaxHeap(input);
console.log(heap);Attempts:
2 left
💡 Hint
Max-heap means parent nodes are always greater than children.
✗ Incorrect
After heapify, the largest element is at the root and the array satisfies max-heap property.
🔧 Debug
advanced2:00remaining
Identify the Bug in Heapify Function
The following heapify function is intended to build a min-heap but produces incorrect output. What is the bug?
DSA Javascript
function heapify(arr, n, i) {
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]];
heapify(arr, n, smallest);
}
}Attempts:
2 left
💡 Hint
Check the comparison signs for min-heap property.
✗ Incorrect
For min-heap, parent should be smaller than children, so comparisons must use '<' to find the smallest child.
🧠 Conceptual
advanced1:30remaining
Time Complexity of Build Heap
What is the time complexity of building a heap from an unsorted array using the heapify method?
Attempts:
2 left
💡 Hint
Consider that heapify is called on all non-leaf nodes and work done decreases at lower levels.
✗ Incorrect
Building a heap using heapify runs in O(n) time because the work done at each level decreases geometrically.
🚀 Application
expert2:30remaining
Resulting Heap After Multiple Insertions
Starting with an empty max-heap, insert the elements [4, 10, 3, 5, 1] one by one. What is the heap array after all insertions?
DSA Javascript
class MaxHeap { constructor() { this.heap = []; } insert(val) { this.heap.push(val); let i = this.heap.length - 1; while (i > 0) { let parent = Math.floor((i - 1) / 2); if (this.heap[parent] < this.heap[i]) { [this.heap[parent], this.heap[i]] = [this.heap[i], this.heap[parent]]; i = parent; } else { break; } } } } const heap = new MaxHeap(); [4, 10, 3, 5, 1].forEach(x => heap.insert(x)); console.log(heap.heap);
Attempts:
2 left
💡 Hint
After each insertion, the new element bubbles up to maintain max-heap property.
✗ Incorrect
Inserting elements one by one and bubbling up results in the heap array [10, 5, 3, 4, 1].