Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to swap two elements in the array.
DSA Rust
arr.swap([1], i);Attempts:
3 left
💡 Hint
Common Mistakes
Using an index out of bounds like n.
Swapping with 0 always, which is incorrect.
✗ Incorrect
The swap function exchanges elements at indices i and another index. Here, swapping with j is correct.
2fill in blank
mediumComplete the code to calculate the left child index in the heap.
DSA Rust
let left = 2 * i + [1];
Attempts:
3 left
💡 Hint
Common Mistakes
Using 2*i instead of 2*i + 1.
Using 2*i + 2 which is for the right child.
✗ Incorrect
In a heap stored in an array, the left child of node i is at 2*i + 1.
3fill in blank
hardFix the error in the condition to check if right child exists in the heap.
DSA Rust
if right < [1] {
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with total array length instead of heap size.
Using wrong variable names like i or left.
✗ Incorrect
The right child index must be less than the current heap size to exist.
4fill in blank
hardFill both blanks to complete the max-heapify comparison and swap.
DSA Rust
if left < heap_size && arr[left] > arr[1] { largest = left; } if right < heap_size && arr[right] > arr[2] { largest = right; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets for indexing.
Comparing with arr[largest] instead of arr[i].
✗ Incorrect
We compare children with the element at index i to find the largest.
5fill in blank
hardFill all three blanks to complete the heap sort main loop.
DSA Rust
for i in (0..[1]).rev() { arr.swap(0, i); heap_size -= [2]; heapify(&mut arr, 0, [3]); }
Attempts:
3 left
💡 Hint
Common Mistakes
Using i as heap size in heapify instead of the updated heap_size variable.
Reducing heap size by wrong amount.
✗ Incorrect
We loop from n down to 1, reduce heap size by 1, and heapify the root with updated heap size.
