What is the purpose of the heapify process when building a heap from an array?
Heapify rearranges elements to satisfy the heap property, ensuring each parent node is greater (max-heap) or smaller (min-heap) than its children.
Click to reveal answer
intermediate
In the context of building a heap, why do we start heapifying from the last non-leaf node?
Because leaf nodes already satisfy the heap property, starting from the last non-leaf node ensures all subtrees are heapified bottom-up efficiently.
Click to reveal answer
intermediate
What is the time complexity of building a heap from an unsorted array using heapify?
The time complexity is O(n), where n is the number of elements in the array.
Click to reveal answer
beginner
Show the Rust function signature for a heapify function that modifies an array slice in place.
fn heapify(arr: &mut [i32], n: usize, i: usize)
Click to reveal answer
beginner
What happens if you heapify a node that already satisfies the heap property?
No changes occur; the function returns quickly because the subtree rooted at that node is already a valid heap.
Click to reveal answer
When building a max-heap from an array, which node index do you start heapifying from?
AThe first leaf node
BThe root node
CThe last non-leaf node
DThe middle node
✗ Incorrect
Heapify starts from the last non-leaf node moving upwards to ensure all subtrees satisfy the heap property.
What is the main goal of the heapify function?
ATo maintain the heap property in a subtree
BTo sort the array
CTo find the maximum element
DTo reverse the array
✗ Incorrect
Heapify ensures the subtree rooted at a given node satisfies the heap property.
What is the time complexity of building a heap from an array using heapify?
AO(n)
BO(n log n)
CO(n^2)
DO(log n)
✗ Incorrect
Building a heap using heapify has a linear time complexity O(n).
In Rust, how do you pass an array to a function to modify it in place?
ABy value
BBy mutable reference
CBy immutable reference
DBy copying
✗ Incorrect
Passing a mutable reference (&mut) allows the function to modify the array in place.
If a node satisfies the heap property, what does heapify do?
ASwaps it with its child
BRecursively heapifies children
CDeletes the node
DDoes nothing and returns
✗ Incorrect
Heapify returns immediately if the node already satisfies the heap property.
Explain step-by-step how to build a max-heap from an unsorted array using heapify.
Think about fixing smaller heaps first before the bigger ones.
You got /4 concepts.
Describe how the heapify function works internally when called on a node.
Focus on comparisons and recursive calls.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the heapify process when building a heap from an array?
easy
A. To find the maximum element in the array
B. To sort the array in ascending order
C. To reverse the array elements
D. To rearrange the array elements to satisfy the heap property
Solution
Step 1: Understand heap property
The heap property requires each parent node to be greater (max-heap) or smaller (min-heap) than its children.
Step 2: Role of heapify
Heapify rearranges elements so this property holds for every subtree, starting from the bottom.
Final Answer:
To rearrange the array elements to satisfy the heap property -> Option D
Quick Check:
Heapify = Rearrange for heap property [OK]
Hint: Heapify fixes subtree order to build a heap fast [OK]
Common Mistakes:
Thinking heapify sorts the array
Confusing heapify with finding max/min element
Assuming heapify reverses the array
2. Which of the following Rust code snippets correctly calls the heapify function on an array slice arr starting at index i with heap size n?
easy
A. heapify(arr, i, n);
B. heapify(&mut arr, i, n);
C. heapify(&arr, i, n);
D. heapify(arr, &i, &n);
Solution
Step 1: Understand Rust borrowing rules
To modify an array in Rust, pass a mutable reference: &mut arr.
Step 2: Check parameter types
Indices i and n are usually copied values, so pass by value.
Final Answer:
heapify(&mut arr, i, n); -> Option B
Quick Check:
Mutable reference for array + values for indices [OK]
Hint: Use &mut for arrays when modifying in Rust [OK]
Common Mistakes:
Passing array without &mut causes compile error
Passing immutable reference &arr when mutation needed
Passing references for integers unnecessarily
3. Given the Rust code below, what will be the state of the array after calling build_heap(&mut arr)? Assume build_heap calls heapify correctly to build a max-heap.
Build_heap calls heapify from last non-leaf node upwards to root, fixing subtree heaps.
Step 2: Apply heapify on array [3,9,2,1,4,5]
Heapify rearranges to max-heap: root is largest, children smaller. Result is [9,4,5,1,3,2].
Final Answer:
[9, 4, 5, 1, 3, 2] -> Option A
Quick Check:
Max-heap root 9, children smaller [OK]
Hint: Build heap from bottom up to fix subtrees [OK]
Common Mistakes:
Assuming array is sorted after build_heap
Confusing max-heap with min-heap output
Not applying heapify from last non-leaf node
4. Consider this Rust function snippet for heapify:
fn heapify(arr: &mut [i32], i: usize, n: 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);
}
}
What is the bug in this code that prevents building a max-heap correctly?
medium
A. The recursive call to heapify uses wrong parameter order
B. The parameters n and i are swapped in the function signature
C. The calculation of left and right child indices is incorrect
D. The function does not check if i is within bounds before accessing arr[i]
Solution
Step 1: Examine function signature
The signature is fn heapify(arr: &mut [i32], i: usize, n: usize).
Step 2: Check recursive call
The recursive call is heapify(arr, n, largest);, which passes n as i and largest as n: wrong parameter order.
Final Answer:
The recursive call to heapify uses wrong parameter order -> Option A
Quick Check:
Recursive call must match signature: arr, i, n [OK]
Hint: Check recursive call matches function parameter order exactly [OK]
Common Mistakes:
Swapping n and i in recursive call
Miscalculating child indices
Ignoring bounds checks for indices
5. You want to build a min-heap from the array [7, 2, 6, 3, 9, 1] using the standard heapify approach. Which modification should you make to the heapify function to correctly build a min-heap instead of a max-heap?
hard
A. Call heapify starting from the root instead of last non-leaf node
B. Swap the left and right child index calculations
C. Change all '>' comparisons to '<' when comparing child and parent values