Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start the binary search with correct initial boundaries.
DSA Rust
let mut left = [1]; let mut right = nums.len() as i32 - 1;
Attempts:
3 left
💡 Hint
Common Mistakes
Starting left at 1 skips the first element.
Using negative indices causes errors.
✗ Incorrect
The search starts from the first index 0 to the last index (length - 1).
2fill in blank
mediumComplete the code to calculate the middle index safely.
DSA Rust
let mid = left + (right - left) [1] 2;
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication instead of division.
Adding left and right directly without dividing.
✗ Incorrect
To find the middle index, we add half the distance between left and right to left.
3fill in blank
hardFix the error in the condition to check if the left half is sorted.
DSA Rust
if nums[left as usize] [1] nums[mid as usize] {
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' excludes equal elements and breaks logic.
Using '<' reverses the condition.
✗ Incorrect
The left half is sorted if the element at left is less than or equal to the element at mid.
4fill in blank
hardFill both blanks to update the search boundaries when target is in the left sorted half.
DSA Rust
if target >= nums[left as usize] && target < nums[mid as usize] { right = [1]; } else { left = [2]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping left and right updates.
Using mid instead of mid ± 1 causes infinite loops.
✗ Incorrect
If target is in left half, move right boundary to mid - 1; else move left boundary to mid + 1.
5fill in blank
hardFill all three blanks to update boundaries when right half is sorted and target is checked.
DSA Rust
if target > nums[mid as usize] && target <= nums[[1] as usize] { left = [2]; } else { right = [3]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using left instead of right in comparison.
Updating boundaries incorrectly causing infinite loops.
✗ Incorrect
Check target against right boundary; if in range, move left to mid + 1; else move right to mid - 1.
