0
0
DSA C++programming~20 mins

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

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!
Predict Output
intermediate
2:00remaining
Output of Min-Heap after Build Heap from Array
What is the state of the array after building a min-heap using heapify on the array [4, 10, 3, 5, 1]?
DSA C++
void heapifyMin(int arr[], int n, int i) {
    int smallest = i;
    int left = 2 * i + 1;
    int 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) {
        std::swap(arr[i], arr[smallest]);
        heapifyMin(arr, n, smallest);
    }
}

void buildMinHeap(int arr[], int n) {
    for (int i = n / 2 - 1; i >= 0; i--) {
        heapifyMin(arr, n, i);
    }
}

int main() {
    int arr[] = {4, 10, 3, 5, 1};
    int n = 5;
    buildMinHeap(arr, n);
    for (int i = 0; i < n; i++) {
        std::cout << arr[i] << " ";
    }
    return 0;
}
A[1, 3, 4, 5, 10]
B[1, 4, 3, 5, 10]
C[3, 4, 1, 5, 10]
D[10, 5, 4, 3, 1]
Attempts:
2 left
💡 Hint
Remember that heapify starts from the last non-leaf node and moves upward, ensuring the min-heap property.
Predict Output
intermediate
2:00remaining
Max-Heap Array after Build Heap
Given the array [2, 7, 6, 3, 9], what is the array after building a max-heap using heapify?
DSA C++
void heapifyMax(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) {
        std::swap(arr[i], arr[largest]);
        heapifyMax(arr, n, largest);
    }
}

void buildMaxHeap(int arr[], int n) {
    for (int i = n / 2 - 1; i >= 0; i--) {
        heapifyMax(arr, n, i);
    }
}

int main() {
    int arr[] = {2, 7, 6, 3, 9};
    int n = 5;
    buildMaxHeap(arr, n);
    for (int i = 0; i < n; i++) {
        std::cout << arr[i] << " ";
    }
    return 0;
}
A[9, 7, 6, 3, 2]
B[7, 9, 6, 3, 2]
C[9, 3, 7, 2, 6]
D[6, 7, 9, 3, 2]
Attempts:
2 left
💡 Hint
Max-heap property means parent nodes are always greater than their children.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Heapify Function
What error will occur when running this heapify function on array [3, 1, 6, 5, 2, 4]?
DSA C++
void heapify(int arr[], int n, int i) {
    int largest = i;
    int left = 2 * i;
    int right = 2 * i + 1;

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

    if (largest != i) {
        std::swap(arr[i], arr[largest]);
        heapify(arr, n, largest);
    }
}
ANo error, outputs max-heap correctly
BCompilation error due to wrong syntax
CStack overflow due to infinite recursion
DArray index out of bounds error
Attempts:
2 left
💡 Hint
Check how left and right child indices are calculated for a zero-based array.
🧠 Conceptual
advanced
1:30remaining
Time Complexity of Build Heap
What is the time complexity of building a heap from an unsorted array of size n using the heapify method?
AO(log n)
BO(n log n)
CO(n)
DO(n^2)
Attempts:
2 left
💡 Hint
Consider that heapify is called on nodes from bottom to top and the work done decreases at lower levels.
🚀 Application
expert
2:30remaining
Final Heap Array after Multiple Heapify Calls
Given the array [8, 4, 7, 1, 3, 5, 6], after calling heapify on index 2 and then on index 0 (max-heap), what is the resulting array?
DSA C++
void heapifyMax(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) {
        std::swap(arr[i], arr[largest]);
        heapifyMax(arr, n, largest);
    }
}

int main() {
    int arr[] = {8, 4, 7, 1, 3, 5, 6};
    int n = 7;
    heapifyMax(arr, n, 2);
    heapifyMax(arr, n, 0);
    for (int i = 0; i < n; i++) {
        std::cout << arr[i] << " ";
    }
    return 0;
}
A[8, 4, 7, 1, 3, 6, 5]
B]6 ,5 ,3 ,1 ,7 ,4 ,8[
C[8, 4, 7, 1, 3, 5, 6]
D]5 ,6 ,3 ,1 ,7 ,4 ,8[
Attempts:
2 left
💡 Hint
Heapify at index 2 swaps elements to maintain max-heap, then heapify at root fixes the heap.