Challenge - 5 Problems
Heap Sort Mastery
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 Java code after performing heap sort on the array?
DSA Java
public class HeapSortExample { public static 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 swap = arr[i]; arr[i] = arr[largest]; arr[largest] = swap; heapify(arr, n, largest); } } public static void heapSort(int[] arr) { int n = arr.length; 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); } } public static void main(String[] args) { int[] arr = {4, 10, 3, 5, 1}; heapSort(arr); for (int num : arr) { System.out.print(num + " "); } } }
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 from the array, then repeatedly swaps the root (largest) with the last element and heapifies the reduced heap. The final output is the array sorted in ascending order.
🧠 Conceptual
intermediateUnderstanding heapify in Heap Sort
In the heap sort algorithm, what is the main purpose of the heapify function?
Attempts:
2 left
💡 Hint
Heapify adjusts the tree to maintain the heap property starting from a node.
✗ Incorrect
Heapify ensures that the subtree rooted at a given index follows the max heap property, meaning the parent node is greater than its children. This is essential for building and maintaining the heap structure during heap sort.
🔧 Debug
advancedIdentify the error in heap sort implementation
What error will occur when running this heap sort code snippet?
DSA Java
public static 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 swap = arr[i]; arr[i] = arr[largest]; arr[largest] = swap; heapify(arr, n, largest); } }
Attempts:
2 left
💡 Hint
Check how left and right child indices are calculated in a heap stored in an array.
✗ Incorrect
In a zero-based index array heap, left child index should be 2*i + 1 and right child 2*i + 2. Using 2*i and 2*i + 1 causes incorrect indexing and may access out-of-bound indices.
🚀 Application
advancedHeap Sort on a large array
Given an array of integers: [12, 11, 13, 5, 6, 7], what will be the state of the array after building the max heap but before sorting?
Attempts:
2 left
💡 Hint
Building max heap rearranges elements so parent nodes are larger than children.
✗ Incorrect
After building the max heap, the array is rearranged to satisfy the heap property. The correct max heap for the array is [13, 11, 12, 6, 5, 7].
🧠 Conceptual
expertTime complexity of Heap Sort
What is the overall time complexity of heap sort and why?
Attempts:
2 left
💡 Hint
Consider the cost of building the heap and the cost of extracting elements.
✗ Incorrect
Building the heap takes O(n) time. Then, extracting the max element n times requires O(log n) each. Total time is O(n) + O(n log n) = O(n log n).
