← Bitkernel Interview Insights
Multiple choice saved me a little here but I still had to work through it.
First, build a max-heap from the given array by applying the heapify process from the last non-leaf node up to the root. Then, perform the first extraction step: swap the root (maximum element) with the last element, reduce the heap size by one, and heapify the root to restore the max-heap property. Finally, present the resulting array after these operations.
Pro tip: Clarify whether the heap is represented as a binary tree or an array, and explicitly state the array indices used for parent-child relationships (e.g., for 0-indexed arrays, children of i are 2i+1 and 2i+2). This shows attention to detail and avoids off-by-one errors.
Starting from the last non-leaf node (index floor(n/2)-1) and moving up to the root, perform sift-down operations to ensure each subtree satisfies the max-heap property. After this, the array becomes a valid max-heap.
The root (index 0) holds the maximum element. The last element is at index n-1. Note their values before swapping.
Exchange the values at index 0 and index n-1. This moves the maximum element to its final sorted position at the end of the array.
Decrease the heap size by 1 (so the last element is excluded from further heap operations). Then, perform sift-down on the new root (index 0) to restore the max-heap property among the remaining elements.
After heapify, the array represents the heap after the first extraction. Present the final array in the order of indices 0 to n-1.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.