Challenge - 5 Problems
Heap Sort Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ā Predict Output
intermediateOutput of Heap Sort on a small array
What is the output of the following heap sort code on the array [4, 10, 3, 5, 1]?
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) { int temp = arr[i]; arr[i] = arr[largest]; arr[largest] = temp; 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--) { int temp = arr[0]; arr[0] = arr[i]; arr[i] = temp; heapify(arr, i, 0); } } int main() { int arr[] = {4, 10, 3, 5, 1}; int n = 5; heapSort(arr, n); for (int i = 0; i < n; i++) { printf("%d ", 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 array is sorted ascending.
š§ Conceptual
intermediateHeap property in heap sort
Which property must the array satisfy after the first for-loop in the heapSort function?
Attempts:
2 left
š” Hint
The first loop builds a heap structure from the array.
ā Incorrect
The first loop builds a max heap from the array, so the array satisfies the max heap property before sorting starts.
š§ Debug
advancedIdentify the bug in heapify function
What error will occur if the heapify function is called with i = -1?
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) { int temp = arr[i]; arr[i] = arr[largest]; arr[largest] = temp; heapify(arr, n, largest); } }
Attempts:
2 left
š” Hint
Negative index access in C arrays is invalid.
ā Incorrect
If i is -1, left and right become negative or invalid indices, causing out of bounds access.
ā Predict Output
advancedHeap sort output with duplicate elements
What is the output of heap sort on the array [2, 3, 3, 1, 2]?
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) { int temp = arr[i]; arr[i] = arr[largest]; arr[largest] = temp; 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--) { int temp = arr[0]; arr[0] = arr[i]; arr[i] = temp; heapify(arr, i, 0); } } int main() { int arr[] = {2, 3, 3, 1, 2}; int n = 5; heapSort(arr, n); for (int i = 0; i < n; i++) { printf("%d ", arr[i]); } return 0; }
Attempts:
2 left
š” Hint
Heap sort sorts duplicates in ascending order as well.
ā Incorrect
Heap sort sorts the array including duplicates in ascending order.
š§ Conceptual
expertTime complexity of heap sort
What is the worst-case time complexity of heap sort for an array of size n?
Attempts:
2 left
š” Hint
Heap sort builds a heap and then extracts elements one by one.
ā Incorrect
Building the heap takes O(n), and each of the n extractions takes O(log n), so total is O(n log n).
