Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to swap two elements in the array.
DSA Rust
fn swap(arr: &mut Vec<i32>, i: usize, j: usize) {
let temp = arr[[1]];
arr[i] = arr[j];
arr[j] = temp;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using j instead of i causes wrong element to be stored.
Using a fixed index like 0 or 1 does not swap the intended elements.
✗ Incorrect
We store the element at index i in temp to swap it with element at index j.
2fill in blank
mediumComplete the code to generate permutations by recursion.
DSA Rust
fn permute(arr: &mut Vec<i32>, start: usize, result: &mut Vec<Vec<i32>>) {
if start == arr.len() {
result.push(arr.clone());
return;
}
for i in start..arr.len() {
swap(arr, start, [1]);
permute(arr, start + 1, result);
swap(arr, start, i);
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Swapping with start instead of i does not change the array.
Using fixed indices causes incorrect permutations.
✗ Incorrect
We swap the element at start with element at i to generate new permutations.
3fill in blank
hardFix the error in the base case condition to stop recursion correctly.
DSA Rust
fn permute(arr: &mut Vec<i32>, start: usize, result: &mut Vec<Vec<i32>>) {
if start [1] arr.len() {
result.push(arr.clone());
return;
}
for i in start..arr.len() {
swap(arr, start, i);
permute(arr, start + 1, result);
swap(arr, start, i);
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using < or > causes infinite recursion or missing permutations.
Using <= causes out-of-bound errors.
✗ Incorrect
The recursion stops when start equals the length of the array, meaning a full permutation is formed.
4fill in blank
hardFill both blanks to initialize the result vector and call the permutation function.
DSA Rust
fn main() {
let mut arr = vec![1, 2, 3];
let mut result = [1];
permute(&mut arr, [2], &mut result);
println!("{:?}", result);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 as start index misses permutations starting at 0.
Using vec![] instead of Vec::new() is valid but here Vec::new() is preferred.
✗ Incorrect
We create an empty vector to store permutations and start recursion from index 0.
5fill in blank
hardFill all three blanks to complete the swap function, recursive call, and print statement.
DSA Rust
fn swap(arr: &mut Vec<i32>, i: usize, j: usize) {
let temp = arr[[1]];
arr[i] = arr[j];
arr[j] = temp;
}
fn permute(arr: &mut Vec<i32>, start: usize, result: &mut Vec<Vec<i32>>) {
if start == arr.len() {
result.push(arr.clone());
return;
}
for i in start..arr.len() {
swap(arr, start, i);
permute(arr, start + 1, result);
swap(arr, start, i);
}
}
fn main() {
let mut arr = vec![1, 2, 3];
let mut result = Vec::new();
permute(&mut arr, [2], &mut result);
println!("{:?}", [3]);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong index in swap causes incorrect swapping.
Starting recursion at wrong index misses permutations.
Printing wrong variable shows incorrect output.
✗ Incorrect
Swap uses i to store temp; recursion starts at 0; print the result vector.
