Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to swap two elements in the array.
DSA Rust
if arr[j] > arr[j + 1] { let temp = arr[j]; arr[j] = arr[j [1] 1]; arr[j + 1] = temp; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' causes index errors.
Using '*' or '/' is not valid for indexing.
✗ Incorrect
We add 1 to j to access the next element for swapping.
2fill in blank
mediumComplete the code to run the outer loop for bubble sort.
DSA Rust
for i in 0..arr.len() [1] 1 { // inner loop }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' causes the loop to run too many times.
Using '*' or '/' is invalid for loop ranges.
✗ Incorrect
We subtract 1 because the last element is already sorted after each pass.
3fill in blank
hardFix the error in the inner loop range to avoid index out of bounds.
DSA Rust
for j in 0..arr.len() [1] i [2] 1 { if arr[j] > arr[j + 1] { arr.swap(j, j + 1); } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' causes index errors.
Using '*' or '/' is invalid for loop ranges.
✗ Incorrect
The inner loop runs until length minus i minus 1 to avoid comparing beyond the sorted part.
4fill in blank
hardFill both blanks to complete the bubble sort function header and return type.
DSA Rust
fn bubble_sort(mut arr: Vec<i32>) -> [1] { let n = arr.len(); for i in 0..n [2] 1 { for j in 0..n - i - 1 { if arr[j] > arr[j + 1] { arr.swap(j, j + 1); } } } arr }
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect return types like i32 or bool.
Using wrong loop range operators.
✗ Incorrect
The function returns a vector of i32 and the loop runs until n minus 1.
5fill in blank
hardFill all three blanks to complete the bubble sort call and print the sorted array.
DSA Rust
fn main() {
let arr = vec![64, 34, 25, 12, 22, 11, 90];
let sorted_arr = bubble_sort([1]);
println!("Sorted array: {:?}", [2]);
for num in [3] {
print!("{} ", num);
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Passing sorted_arr to bubble_sort causes infinite recursion.
Iterating over the original array instead of the sorted one.
✗ Incorrect
We pass 'arr' to bubble_sort, print 'sorted_arr', and iterate over 'sorted_arr.iter()' to print elements.
