0
0
DSA C++programming~10 mins

Build Heap from Array Heapify in DSA C++ - Interactive Practice

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

Complete the code to get the index of the last non-leaf node in the heap array.

DSA C++
int lastNonLeaf = (n [1] 2) - 1;
Drag options to blanks, or click blank then click option'
A/
B*
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication instead of division.
Forgetting to subtract 1 after division.
2fill in blank
medium

Complete the code to call heapify on each non-leaf node starting from the last non-leaf node.

DSA C++
for (int i = lastNonLeaf; i >= 0; i--) {
    heapify(arr, n, [1]);
}
Drag options to blanks, or click blank then click option'
Ai
Bn
C0
DlastNonLeaf
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the size n instead of the node index.
Passing 0 or lastNonLeaf instead of the current index.
3fill in blank
hard

Fix the error in the heapify function call inside the buildHeap function.

DSA C++
void buildHeap(int arr[], int n) {
    int startIdx = (n / 2) - 1;
    for (int i = startIdx; i >= 0; i--) {
        heapify(arr, [1], i);
    }
}
Drag options to blanks, or click blank then click option'
Ai
Bn
CstartIdx
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the current index i as the size parameter.
Confusing the order of parameters in heapify.
4fill in blank
hard

Fill both blanks to complete the heapify function's left and right child index calculations.

DSA C++
int left = 2 * [1] + [2];
int right = 2 * [1] + 2;
Drag options to blanks, or click blank then click option'
Ai
B1
C0
Dn
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of 1 for left child offset.
Using n or other variables instead of i.
5fill in blank
hard

Fill all three blanks to complete the heapify function's swap and recursive call.

DSA C++
if (largest != [1]) {
    std::swap(arr[[2]], arr[[3]]);
    heapify(arr, n, largest);
}
Drag options to blanks, or click blank then click option'
Ai
Blargest
Dn
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping wrong indices.
Not calling heapify recursively after swap.