Challenge - 5 Problems
Heap Sort Master
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 Go code that performs heap sort on the array [4, 10, 3, 5, 1]?
DSA Go
package main import "fmt" func heapify(arr []int, n int, i int) { largest := i l := 2*i + 1 r := 2*i + 2 if l < n && arr[l] > arr[largest] { largest = l } if r < n && arr[r] > arr[largest] { largest = r } if largest != i { arr[i], arr[largest] = arr[largest], arr[i] heapify(arr, n, largest) } } func heapSort(arr []int) { n := len(arr) for i := n/2 - 1; i >= 0; i-- { heapify(arr, n, i) } for i := n - 1; i >= 0; i-- { arr[0], arr[i] = arr[i], arr[0] heapify(arr, i, 0) } } func main() { arr := []int{4, 10, 3, 5, 1} heapSort(arr) fmt.Println(arr) }
Attempts:
2 left
💡 Hint
Heap sort sorts the array in ascending order by building a max heap first.
✗ Incorrect
The heap sort algorithm first builds a max heap, then repeatedly swaps the root with the last element and heapifies the reduced heap. This results in a sorted array in ascending order.
🧠 Conceptual
intermediate1:30remaining
Heap Sort Time Complexity Explanation
Which of the following best describes the 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, and then removing the largest element n times takes O(n log n) total. So overall time complexity is O(n log n).
🔧 Debug
advanced2:00remaining
Identify the bug in heapify function
What error will occur when running this heapify function on an array, and why?
DSA Go
func heapify(arr []int, n int, i int) { largest := i l := 2*i + 1 r := 2*i + 2 if l < n && arr[l] > arr[largest] { largest = l } if r < n && arr[r] > arr[largest] { largest = r } if largest != i { arr[i], arr[largest] = arr[largest], arr[i] 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
The left and right child indices in a zero-based array heap should be 2*i + 1 and 2*i + 2. Using 2*i and 2*i + 1 causes out of range access.
🚀 Application
advanced1:30remaining
Heap Sort Stability
Is heap sort a stable sorting algorithm? Choose the correct explanation.
Attempts:
2 left
💡 Hint
Consider what happens when equal elements are swapped during heapify.
✗ Incorrect
Heap sort is not stable because it swaps elements that may be far apart, which can change the relative order of equal elements.
❓ Predict Output
expert2:30remaining
Heap Sort Output with Duplicate Elements
What is the output of the following Go code after heap sorting the array [2, 3, 3, 1, 2]?
DSA Go
package main import "fmt" func heapify(arr []int, n int, i int) { largest := i l := 2*i + 1 r := 2*i + 2 if l < n && arr[l] > arr[largest] { largest = l } if r < n && arr[r] > arr[largest] { largest = r } if largest != i { arr[i], arr[largest] = arr[largest], arr[i] heapify(arr, n, largest) } } func heapSort(arr []int) { n := len(arr) for i := n/2 - 1; i >= 0; i-- { heapify(arr, n, i) } for i := n - 1; i >= 0; i-- { arr[0], arr[i] = arr[i], arr[0] heapify(arr, i, 0) } } func main() { arr := []int{2, 3, 3, 1, 2} heapSort(arr) fmt.Println(arr) }
Attempts:
2 left
💡 Hint
Heap sort sorts all elements in ascending order regardless of duplicates.
✗ Incorrect
Heap sort will sort the array in ascending order including duplicates, resulting in [1 2 2 3 3].