Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to get the index of the left child in a heap array.
DSA Rust
fn left_child(index: usize) -> usize {
index [1]
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 2 * index instead of 2 * index + 1.
Using + 2 instead of + 1.
✗ Incorrect
In a heap stored as an array, the left child of a node at index i is at 2*i + 1.
2fill in blank
mediumComplete the code to swap two elements in a vector.
DSA Rust
fn swap(heap: &mut Vec<i32>, i: usize, j: usize) {
let temp = heap[i];
heap[i] = heap[[1]];
heap[j] = temp;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using the same index for both assignments.
Using an invalid index like 0 or heap.len() - 1.
✗ Incorrect
To swap elements at i and j, assign heap[i] = heap[j] and heap[j] = temp.
3fill in blank
hardFix the error in the bubble down loop condition to avoid out-of-bounds access.
DSA Rust
while child_index [1] heap.len() { // bubble down logic }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causes index out of bounds error.
Using '>' or '>=' reverses the logic.
✗ Incorrect
The child index must be strictly less than heap length to be valid.
4fill in blank
hardFill both blanks to correctly update the parent and child indices during bubble down.
DSA Rust
parent_index = [1]; child_index = [2];
Attempts:
3 left
💡 Hint
Common Mistakes
Not updating indices correctly causes infinite loops.
Using wrong functions for child index.
✗ Incorrect
After swapping, parent moves to child's position, and child is updated to left child of new parent.
5fill in blank
hardFill all three blanks to complete the bubble down step choosing the correct child to swap with.
DSA Rust
if right_child_index [1] heap.len() && heap[right_child_index] < heap[left_child_index] { child_index = [2]; } else { child_index = [3]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' reverses min-heap logic.
Assigning wrong child index causes incorrect swaps.
✗ Incorrect
We pick the smaller child for min-heap bubble down. If right child is smaller, choose it; else left child.
