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 Rust code after performing heap sort on the array?
DSA Rust
fn heapify(arr: &mut [i32], n: usize, i: usize) {
let mut largest = i;
let l = 2 * i + 1;
let 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.swap(i, largest);
heapify(arr, n, largest);
}
}
fn heap_sort(arr: &mut [i32]) {
let n = arr.len();
for i in (0..n / 2).rev() {
heapify(arr, n, i);
}
for i in (1..n).rev() {
arr.swap(0, i);
heapify(arr, i, 0);
}
}
fn main() {
let mut arr = [4, 10, 3, 5, 1];
heap_sort(&mut arr);
println!("{:?}", arr);
}Attempts:
2 left
💡 Hint
Remember heap sort sorts the array in ascending order.
✗ Incorrect
Heap sort builds a max heap and then extracts the largest element repeatedly, resulting in a sorted array in ascending order.
🧠 Conceptual
intermediateHeap 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 extracting each element takes O(log n) time, repeated n times, resulting in O(n log n) overall.
🔧 Debug
advancedIdentify the bug in this heapify function
What error will this Rust heapify function cause when run on an array?
DSA Rust
fn heapify(arr: &mut [i32], n: usize, i: usize) {
let mut largest = i;
let l = 2 * i + 1;
let 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.swap(i, largest);
heapify(arr, n, largest);
}
}Attempts:
2 left
💡 Hint
Check the conditions for l and r compared to n.
✗ Incorrect
The conditions use <= n but valid indices are less than n, so accessing arr[n] causes panic.
❓ Predict Output
advancedHeap Sort on a reversed array
What is the output of the following Rust code after heap sorting the reversed array?
DSA Rust
fn heapify(arr: &mut [i32], n: usize, i: usize) {
let mut largest = i;
let l = 2 * i + 1;
let 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.swap(i, largest);
heapify(arr, n, largest);
}
}
fn heap_sort(arr: &mut [i32]) {
let n = arr.len();
for i in (0..n / 2).rev() {
heapify(arr, n, i);
}
for i in (1..n).rev() {
arr.swap(0, i);
heapify(arr, i, 0);
}
}
fn main() {
let mut arr = [9, 7, 5, 3, 1];
heap_sort(&mut arr);
println!("{:?}", arr);
}Attempts:
2 left
💡 Hint
Heap sort always sorts in ascending order.
✗ Incorrect
Heap sort rearranges the reversed array into ascending order.
🧠 Conceptual
expertWhy is heap sort preferred over quicksort in worst-case scenarios?
Which statement best explains why heap sort is preferred over quicksort when worst-case time complexity matters?
Attempts:
2 left
💡 Hint
Consider worst-case time complexities of both algorithms.
✗ Incorrect
Heap sort has a guaranteed O(n log n) worst-case time, while quicksort can degrade to O(n^2) depending on pivot choices.
