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 list
What is the output of the following Python code that uses heap sort to sort a list?
DSA Python
def heapify(arr, n, i): largest = i left = 2 * i + 1 right = 2 * i + 2 if left < n and arr[left] > arr[largest]: largest = left if right < n and arr[right] > arr[largest]: largest = right if largest != i: arr[i], arr[largest] = arr[largest], arr[i] heapify(arr, n, largest) def heap_sort(arr): n = len(arr) for i in range(n // 2 - 1, -1, -1): heapify(arr, n, i) for i in range(n - 1, 0, -1): arr[i], arr[0] = arr[0], arr[i] heapify(arr, i, 0) arr = [4, 10, 3, 5, 1] heap_sort(arr) print(arr)
Attempts:
2 left
💡 Hint
Remember heap sort sorts the list in ascending order by building a max heap first.
✗ Incorrect
The code builds a max heap and then extracts the largest element repeatedly, placing it at the end. This results in a sorted list in ascending order.
🧠 Conceptual
intermediateHeap property in heap sort
In heap sort, what property does the heap maintain during the sorting process?
Attempts:
2 left
💡 Hint
Heap sort uses a max heap to find the largest element quickly.
✗ Incorrect
Heap sort builds a max heap so the largest element is always at the root, which helps in sorting the array.
🔧 Debug
advancedIdentify the error in heapify function
What error will occur when running this heapify function on an array?
DSA Python
def heapify(arr, n, i): largest = i left = 2 * i + 1 right = 2 * i + 2 if left <= n and arr[left] > arr[largest]: largest = left if right <= n and arr[right] > arr[largest]: largest = right if largest != i: arr[i], arr[largest] = arr[largest], arr[i] heapify(arr, n, largest)
Attempts:
2 left
💡 Hint
Check the boundary conditions for left and right indices.
✗ Incorrect
The conditions use <= n which allows indices equal to n, but valid indices are less than n. This causes IndexError.
🚀 Application
advancedHeap sort time complexity
What is the time complexity of heap sort in the worst case?
Attempts:
2 left
💡 Hint
Heap sort builds a heap and then extracts elements one by one.
✗ Incorrect
Heap sort builds a heap in O(n) and then performs n extractions each taking O(log n), resulting in O(n log n) overall.
❓ Predict Output
expertOutput after partial heap sort steps
Given the following code snippet, what is the state of the array after the first for loop completes (building the max heap)?
DSA Python
def heapify(arr, n, i): largest = i left = 2 * i + 1 right = 2 * i + 2 if left < n and arr[left] > arr[largest]: largest = left if right < n and arr[right] > arr[largest]: largest = right if largest != i: arr[i], arr[largest] = arr[largest], arr[i] heapify(arr, n, largest) def heap_sort(arr): n = len(arr) for i in range(n // 2 - 1, -1, -1): heapify(arr, n, i) arr = [3, 9, 2, 1, 4, 5] heap_sort(arr) print(arr)
Attempts:
2 left
💡 Hint
The first loop builds a max heap from the array.
✗ Incorrect
After heapify calls, the array is rearranged to satisfy max heap property: [9, 4, 5, 1, 3, 2].
