Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to swap elements between two arrays.
DSA Rust
if arr1[i] > arr2[[1]] { let temp = arr1[i]; arr1[i] = arr2[j]; arr2[j] = temp; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using i instead of j for arr2 index causes wrong element access.
Using 0 or len(arr2) - 1 ignores the current position in arr2.
✗ Incorrect
We compare arr1[i] with arr2[j], so the index to access arr2 is j.
2fill in blank
mediumComplete the code to perform the final sorting of the first array.
DSA Rust
arr1.sort_by(|a, b| a[1]b);Attempts:
3 left
💡 Hint
Common Mistakes
Using eq or ne returns bool, not Ordering, causing compile errors.
Using partial_cmp returns Option, which is less direct.
✗ Incorrect
The cmp method compares two values for sorting in Rust.
3fill in blank
hardFix the error in the loop condition to correctly iterate over arr1.
DSA Rust
for i in 0..[1] { // loop body }
Attempts:
3 left
💡 Hint
Common Mistakes
Using arr2.len() causes index out of bounds if arr2 is smaller.
Using arr1.len() - 1 misses the last element.
✗ Incorrect
We iterate over all elements of arr1, so use arr1.len() as the upper bound.
4fill in blank
hardFill both blanks to correctly swap and then sort both arrays.
DSA Rust
if arr1[[1]] > arr2[[2]] { let temp = arr1[i]; arr1[i] = arr2[j]; arr2[j] = temp; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or len(arr2) - 1 ignores current indices.
Swapping with wrong indices causes incorrect order.
✗ Incorrect
We compare arr1[i] and arr2[j], so use i and j as indices respectively.
5fill in blank
hardFill the blanks to complete the merge logic without extra space.
DSA Rust
let mut i = 0; let mut j = 0; while i < [1] && j < [2] { if arr1[i] > arr2[j] { let temp = arr1[i]; arr1[i] = arr2[j]; arr2[j] = temp; j += 1; } else { i += 1; } } arr1.sort(); arr2.sort();
Attempts:
3 left
💡 Hint
Common Mistakes
Using len() - 1 causes missing last element.
Using wrong array length causes index errors.
✗ Incorrect
We loop while i < arr1.len() and j < arr2.len(), and increment j inside the loop.
