Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add a new element at the end of the heap vector.
DSA Rust
heap.push([1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using 'index' or 'parent' instead of the actual value.
Trying to push the entire heap again.
✗ Incorrect
We push the new value to the end of the heap vector before bubbling up.
2fill in blank
mediumComplete the code to find the parent index of the current node during bubble up.
DSA Rust
let parent = ([1] - 1) / 2;
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'value' or 'heap.len()' instead of the current index.
Using addition instead of subtraction.
✗ Incorrect
The parent index is calculated from the current index using (index - 1) / 2.
3fill in blank
hardFix the error in the condition to continue bubbling up while the current node is greater than its parent.
DSA Rust
while index > 0 && heap[[1]] > heap[parent] {
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing parent with itself or wrong index.
Using 'value' or 'heap.len()' as index.
✗ Incorrect
We compare the current node at 'index' with its parent to decide if we swap.
4fill in blank
hardFill both blanks to swap the current node with its parent and update the index.
DSA Rust
heap.swap([1], [2]); index = [3];
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping wrong indices or updating index incorrectly.
Using 'value' or 'heap.len()' in swap or index update.
✗ Incorrect
Swap the current node at 'index' with its 'parent', then update 'index' to parent's position.
5fill in blank
hardFill all three blanks to complete the bubble up loop: calculate parent, check condition, and update parent.
DSA Rust
while index > 0 && heap[index] > heap[[1]] { heap.swap(index, [2]); index = [3]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong parent calculation or updating index incorrectly.
Swapping with wrong indices.
✗ Incorrect
Calculate parent as (index - 1) / 2, swap with parent, then update index to parent.
