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
beginner
In a zero-based array representation of a heap, what are the indices of the left and right children of the node at index i?
Left child index = 2 * i + 1, Right child index = 2 * i + 2.
Click to reveal answer
intermediate
Why do we start heapifying from the last non-leaf node when building a heap from an array?
Because leaf nodes already satisfy the heap property, starting from the last non-leaf node ensures all subtrees become heaps efficiently.
Click to reveal answer
intermediate
What is the time complexity of building a heap using the heapify method on an array of size n?
The time complexity is O(n), which is more efficient than repeatedly inserting elements (O(n log n)).
Click to reveal answer
beginner
Explain the difference between max-heapify and min-heapify.
Max-heapify ensures each parent node is greater than its children, while min-heapify ensures each parent node is smaller than its children.
Click to reveal answer
What is the index of the last non-leaf node in a heap array of size n?
A0
Bn - 1
CMath.floor(n / 2) - 1
DMath.floor(n / 2)
✗ Incorrect
The last non-leaf node is at index Math.floor(n / 2) - 1 because nodes after this are leaves.
Which of the following best describes the heapify operation?
ARearranging a subtree to maintain heap property
BInserting a new element into the heap
CRemoving the root element from the heap
DSorting the entire array
✗ Incorrect
Heapify fixes the heap property in a subtree rooted at a given node.
What is the time complexity of building a heap from an unsorted array using heapify?
AO(n log n)
BO(log n)
CO(n^2)
DO(n)
✗ Incorrect
Building a heap using heapify runs in O(n) time.
In a max-heap, what is true about the root element?
AIt is the smallest element
BIt is the largest element
CIt is the average of all elements
DIt is always zero
✗ Incorrect
The root of a max-heap is the largest element.
When building a heap, why do we heapify from bottom to top?
ABecause leaf nodes are already heaps
BTo sort the array immediately
CTo insert new elements faster
DTo remove duplicates
✗ Incorrect
Leaf nodes satisfy heap property, so heapifying bottom-up fixes all subtrees efficiently.
Describe the steps to build a max-heap from an unsorted array using heapify.
Think about fixing subtrees from bottom to top.
You got /4 concepts.
Explain why building a heap using heapify is more efficient than inserting elements one by one.
Compare time complexities and approach.
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 reverse the array elements
B. To sort the array in ascending order
C. To find the maximum element in the array
D. To rearrange the array elements to satisfy the heap property
Solution
Step 1: Understand heap property
A heap is a special tree where each parent node is greater (max-heap) or smaller (min-heap) than its children.
Step 2: Role of heapify
Heapify rearranges elements so the heap property holds for every subtree, starting from a node downwards.
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 to maintain heap order [OK]
Common Mistakes:
Thinking heapify sorts the array
Confusing heapify with finding max element
Assuming heapify reverses the array
2. Which of the following is the correct way to start heapifying an array arr of length n in JavaScript to build a max heap?
easy
A. for(let i = 0; i < n; i++) heapify(arr, i, n);
B. for(let i = 1; i < n; i++) heapify(arr, i, n);
C. for(let i = Math.floor(n/2) - 1; i >= 0; i--) heapify(arr, i, n);
D. for(let i = n - 1; i >= 0; i--) heapify(arr, i, n);
Solution
Step 1: Identify heapify start index
Heapify starts from the last non-leaf node, which is at index Math.floor(n/2) - 1.
Step 2: Loop backwards to root
We heapify each node from this index down to 0 to ensure the entire tree satisfies heap property.
Final Answer:
for(let i = Math.floor(n/2) - 1; i >= 0; i--) heapify(arr, i, n); -> Option C
Quick Check:
Start heapify at last non-leaf node [OK]
Hint: Start heapify from last parent node backwards [OK]
Common Mistakes:
Starting heapify from index 0 forwards
Heapifying leaf nodes unnecessarily
Using incorrect loop boundaries
3. Given the array [3, 9, 2, 1, 4, 5], what is the array after building a max heap using heapify?
medium
A. [9, 4, 5, 1, 3, 2]
B. [9, 4, 5, 3, 1, 2]
C. [5, 4, 9, 1, 3, 2]
D. [9, 3, 5, 1, 4, 2]
Solution
Step 1: Start heapify from index 2 (Math.floor(6/2)-1)
Heapify at index 2: children are indices 5 (5) and no right child. 2 < 5, swap to get [3,9,5,1,4,2].
Step 2: Heapify at index 1
Children at indices 3 (1) and 4 (4). 9 > 4 and 1, no swap needed.
Step 3: Heapify at index 0
Children at indices 1 (9) and 2 (5). 3 < 9, swap 3 and 9: [9,3,5,1,4,2]. Heapify at index 1 again: children 3 (1) and 4 (4). 3 < 4, swap: [9,4,5,1,3,2].
Final Answer:
[9, 4, 5, 1, 3, 2] -> Option A
Quick Check:
Max heap root is 9, children smaller [OK]
Hint: Heapify bottom-up swaps to fix heap [OK]
Common Mistakes:
Not heapifying children after swap
Swapping with wrong child
Stopping heapify too early
4. Consider this heapify function snippet in JavaScript:
function heapify(arr, i, n) {
let largest = i;
let left = 2 * i + 1;
let 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) {
[arr[i], arr[largest]] = [arr[largest], arr[i]];
heapify(arr, largest, n);
}
}
What is the bug if the function is called with heapify(arr, 0, arr.length - 1) instead of heapify(arr, 0, arr.length)?
medium
A. It will ignore the last element of the array during heapify
B. It will cause an infinite recursion
C. It will throw a syntax error
D. It will heapify the entire array correctly
Solution
Step 1: Understand parameter n meaning
Parameter n is the array length limit for heapify to avoid out-of-bound access.
Step 2: Effect of passing arr.length - 1
Passing arr.length - 1 excludes the last element index (n-1) from heapify checks, so last element is ignored.
Final Answer:
It will ignore the last element of the array during heapify -> Option A
Quick Check:
n must be array length, not length-1 [OK]
Hint: Use full array length as heapify limit [OK]
Common Mistakes:
Confusing length and last index
Expecting syntax error instead of logic error
Ignoring off-by-one errors
5. You have an array [4, 10, 3, 5, 1] and want to build a min heap using heapify. Which of the following changes is necessary in the heapify function to build a min heap instead of a max heap?
hard
A. Swap the left and right child indices calculations
B. Change comparisons from '>' to '<' when comparing children with parent
C. Change comparisons from '<' to '>' when comparing children with parent
D. No change needed; heapify works for both min and max heaps
Solution
Step 1: Understand max vs min heap comparison
Max heap keeps parent >= children; min heap keeps parent <= children.
Step 2: Adjust comparison operators
To build min heap, change '>' to '<' so parent is smaller than children.
Final Answer:
Change comparisons from '>' to '<' when comparing children with parent -> Option B
Quick Check:
Min heap uses less-than comparisons [OK]
Hint: Flip comparison signs for min heap [OK]
Common Mistakes:
Swapping child indices instead of comparisons
Leaving comparisons unchanged
Assuming heapify auto-detects heap type
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.