Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to reverse the array using the built-in method.
DSA Rust
let mut arr = [1, 2, 3, 4, 5]; arr.[1](); println!("{:?}", arr);
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sort' which arranges elements in order, not reverses.
Using 'push' or 'pop' which are for vectors, not arrays.
✗ Incorrect
The built-in method to reverse an array in Rust is 'reverse()'. It reverses the elements in place.
2fill in blank
mediumComplete the code to swap elements at positions i and j in the array.
DSA Rust
let mut arr = [10, 20, 30, 40]; let i = 0; let j = 3; arr.[1](i, j); println!("{:?}", arr);
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'reverse' which reverses the whole array, not swap two elements.
Using 'clone' which copies the array.
✗ Incorrect
The 'swap' method exchanges elements at two indices in the array.
3fill in blank
hardFix the error in the loop to reverse the array manually.
DSA Rust
let mut arr = [1, 2, 3, 4, 5]; let n = arr.len(); for i in 0..n / 2 { arr.[1](i, n - i - 1); } println!("{:?}", arr);
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'reverse' inside the loop which is incorrect.
Using 'push' which is not valid for arrays.
✗ Incorrect
To reverse manually, swap elements from start and end moving towards the center using 'swap'.
4fill in blank
hardFill both blanks to create a reversed vector from the array.
DSA Rust
let arr = [1, 2, 3, 4]; let rev_vec: Vec<_> = arr.iter().[1]().[2]().collect(); println!("{:?}", rev_vec);
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'cloned' instead of 'copied' which works but is less efficient for Copy types.
Using 'sorted' which sorts instead of reversing.
✗ Incorrect
First reverse the iterator with 'rev()', then copy elements with 'copied()' to get owned values for the vector.
5fill in blank
hardFill all three blanks to reverse the array using a for loop and temporary variable.
DSA Rust
let mut arr = [5, 6, 7, 8]; let n = arr.len(); for i in 0..n / 2 { let temp = arr[[1]]; arr[[2]] = arr[[3]]; arr[[3]] = temp; } println!("{:?}", arr);
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'n - i' which is out of bounds.
Mixing up indices causing incorrect swaps.
✗ Incorrect
Use 'i' for the first index, assign arr[i] = arr[n - i - 1], and arr[n - i - 1] = temp to swap manually.
