Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to swap elements at two indices in the vector.
DSA Rust
nums.swap([1], right);Attempts:
3 left
💡 Hint
Common Mistakes
Swapping with 'right' instead of 'left' swaps wrong elements.
Using an index variable not defined in this scope.
✗ Incorrect
We swap the element at 'left' with the element at 'right' to move 2s to the end.
2fill in blank
mediumComplete the code to decrease the 'right' pointer after swapping a 2.
DSA Rust
right [1] 1;
Attempts:
3 left
💡 Hint
Common Mistakes
Increasing 'right' causes out-of-bound errors.
Using multiplication or division operators instead of subtraction.
✗ Incorrect
We decrease 'right' by 1 to move the pointer leftwards after placing a 2.
3fill in blank
hardFix the error in the loop condition to continue while 'left' is less than or equal to 'right'.
DSA Rust
while left [1] right {
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' misses processing when pointers meet.
Using '>' or '>=' causes the loop to never run.
✗ Incorrect
The loop runs while 'left' is less than or equal to 'right' to cover all elements.
4fill in blank
hardFill both blanks to correctly handle the case when nums[left] == 0.
DSA Rust
if nums[left] == 0 { nums.swap([1], left); [2] += 1; left += 1; }
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping with 'right' here is incorrect.
Incrementing a variable other than 'left' causes logic errors.
✗ Incorrect
We swap nums[left] with nums[left] (which is itself) and increment 'left' pointer.
5fill in blank
hardFill all three blanks to correctly handle the case when nums[left] == 2.
DSA Rust
if nums[left] == 2 { nums.swap(left, [1]); [2] -= 1; // Do not increment left here }
Attempts:
3 left
💡 Hint
Common Mistakes
Incrementing 'left' after swapping 2 causes skipping elements.
Swapping with 'left' instead of 'right' is incorrect.
✗ Incorrect
We swap nums[left] with nums[right], then decrement 'right'. We do not increment 'left' here.
