Challenge - 5 Problems
Heap Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output array after heapifying?
Given the array [4, 10, 3, 5, 1], what is the array after building a max heap using heapify?
DSA C
int arr[] = {4, 10, 3, 5, 1}; int n = 5; // Build max heap code runs here // Output the array after heapify
Attempts:
2 left
💡 Hint
Remember max heap means parent nodes are greater than children.
✗ Incorrect
Heapify rearranges the array so that the largest element is at the root and the heap property is maintained for all nodes.
❓ Predict Output
intermediateWhat is the output array after building a min heap?
Given the array [7, 2, 6, 3, 9], what is the array after building a min heap using heapify?
DSA C
int arr[] = {7, 2, 6, 3, 9}; int n = 5; // Build min heap code runs here // Output the array after heapify
Attempts:
2 left
💡 Hint
Min heap means parent nodes are smaller than children.
✗ Incorrect
Heapify rearranges the array so that the smallest element is at the root and the heap property is maintained for all nodes.
🧠 Conceptual
advancedWhy do we start heapify from the last non-leaf node?
In building a heap from an array, why do we start heapify from the last non-leaf node and move upwards?
Attempts:
2 left
💡 Hint
Think about which nodes have children.
✗ Incorrect
Leaf nodes have no children, so they already satisfy the heap property. Starting from the last non-leaf node ensures all subtrees become heaps.
🔧 Debug
advancedWhat error occurs in this heapify code snippet?
Consider this C code snippet for heapify:
void heapify(int arr[], int n, int i) {
int largest = i;
int left = 2*i + 1;
int 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) {
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
}
}
What error will happen if n is zero?
Attempts:
2 left
💡 Hint
Check the conditions before accessing array elements.
✗ Incorrect
If n is zero, left and right will be greater than or equal to n, so no recursive call happens and function returns safely.
🚀 Application
expertHow many swaps are needed to build a max heap from [1, 3, 5, 7, 9, 2]?
Using the standard heapify method to build a max heap from the array [1, 3, 5, 7, 9, 2], how many swaps will occur during the entire build heap process?
Attempts:
2 left
💡 Hint
Simulate heapify from last non-leaf node upwards and count swaps.
✗ Incorrect
Heapify swaps occur when a parent is smaller than a child. Counting swaps carefully during build heap gives 3 swaps total.
