Challenge - 5 Problems
Heap Sort Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Heap Sort on a small array
What is the output of the following C++ code after performing heap sort on the array?
DSA C++
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) { std::swap(arr[i], arr[largest]); heapify(arr, n, largest); } } void heapSort(int arr[], int n) { for (int i = n / 2 - 1; i >= 0; i--) heapify(arr, n, i); for (int i = n - 1; i > 0; i--) { std::swap(arr[0], arr[i]); heapify(arr, i, 0); } } int main() { int arr[] = {4, 10, 3, 5, 1}; int n = sizeof(arr) / sizeof(arr[0]); heapSort(arr, n); for (int i = 0; i < n; i++) { std::cout << arr[i] << " "; } return 0; }
Attempts:
2 left
💡 Hint
Heap sort sorts the array in ascending order by building a max heap first.
✗ Incorrect
Heap sort first builds a max heap, then repeatedly swaps the root with the last element and heapifies the reduced heap. The final output is the array sorted in ascending order.
🧠 Conceptual
intermediate1:30remaining
Understanding heapify function behavior
What is the main purpose of the heapify function in the heap sort algorithm?
Attempts:
2 left
💡 Hint
Heapify ensures the heap property for a subtree rooted at a given index.
✗ Incorrect
Heapify rearranges elements to maintain the max heap property for the subtree rooted at the given index, ensuring the parent node is larger than its children.
❓ Predict Output
advanced2:30remaining
Output after partial heap sort steps
Given the array {7, 2, 6, 3, 9} and the heap sort algorithm, what is the array state after the first swap and heapify in the sorting phase?
DSA C++
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) { std::swap(arr[i], arr[largest]); heapify(arr, n, largest); } } void heapSort(int arr[], int n) { for (int i = n / 2 - 1; i >= 0; i--) heapify(arr, n, i); std::swap(arr[0], arr[n-1]); heapify(arr, n-1, 0); } int main() { int arr[] = {7, 2, 6, 3, 9}; int n = sizeof(arr) / sizeof(arr[0]); heapSort(arr, n); for (int i = 0; i < n; i++) { std::cout << arr[i] << " "; } return 0; }
Attempts:
2 left
💡 Hint
After building the max heap, the largest element is swapped with the last element, then heapify is called on the reduced heap.
✗ Incorrect
Initial max heap is built: {9, 7, 6, 3, 2}. Swap root (9) with last element (2) gives {2, 7, 6, 3, 9}. Heapify on first 4 elements rearranges to {7, 3, 6, 2, 9}.
🔧 Debug
advanced2:00remaining
Identify the error in heapify implementation
What error will the following heapify function cause when called during heap sort?
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); } }
Attempts:
2 left
💡 Hint
Check how left and right child indices are calculated for a zero-based array.
✗ Incorrect
The left and right child indices are incorrectly calculated for zero-based indexing. They should be 2*i + 1 and 2*i + 2. Using 2*i and 2*i + 1 can cause accessing invalid indices leading to out of bounds errors.
🚀 Application
expert1:30remaining
Heap sort stability and behavior
Which statement about heap sort is true?
Attempts:
2 left
💡 Hint
Consider how heap sort swaps elements during sorting.
✗ Incorrect
Heap sort is not stable because it swaps elements that may be equal, changing their original order. It sorts in ascending order by default and works in-place without extra memory proportional to input size.