Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to find the floor value in a sorted array.
DSA Rust
fn floor_in_sorted(arr: &[i32], target: i32) -> Option<i32> {
let mut low = 0;
let mut high = arr.len() as i32 - 1;
let mut result = None;
while low <= high {
let mid = low + (high - low) / 2;
if arr[mid as usize] == target {
return Some(arr[mid as usize]);
} else if arr[mid as usize] < target {
result = Some(arr[mid as usize]);
low = mid + [1];
} else {
high = mid - 1;
}
}
result
}Attempts:
3 left
💡 Hint
Common Mistakes
Using low = mid instead of low = mid + 1 causes infinite loops.
Using negative increments for low pointer.
✗ Incorrect
When the middle element is less than the target, we move the low pointer to mid + 1 to search the right half.
2fill in blank
mediumComplete the code to find the ceil value in a sorted array.
DSA Rust
fn ceil_in_sorted(arr: &[i32], target: i32) -> Option<i32> {
let mut low = 0;
let mut high = arr.len() as i32 - 1;
let mut result = None;
while low <= high {
let mid = low + (high - low) / 2;
if arr[mid as usize] == target {
return Some(arr[mid as usize]);
} else if arr[mid as usize] > target {
result = Some(arr[mid as usize]);
high = mid - [1];
} else {
low = mid + 1;
}
}
result
}Attempts:
3 left
💡 Hint
Common Mistakes
Using high = mid instead of high = mid - 1 causes infinite loops.
Using positive increments for high pointer.
✗ Incorrect
When the middle element is greater than the target, we move the high pointer to mid - 1 to search the left half.
3fill in blank
hardFix the error in the binary search loop condition to avoid infinite loops.
DSA Rust
fn binary_search(arr: &[i32], target: i32) -> Option<usize> {
let mut low = 0;
let mut high = arr.len() as i32 - 1;
while low [1] high {
let mid = low + (high - low) / 2;
if arr[mid as usize] == target {
return Some(mid as usize);
} else if arr[mid as usize] < target {
low = mid + 1;
} else {
high = mid - 1;
}
}
None
}Attempts:
3 left
💡 Hint
Common Mistakes
Using < causes missing the last element in search.
Using > or >= causes logic errors.
✗ Incorrect
The loop should continue while low is less than or equal to high to cover all elements.
4fill in blank
hardFill both blanks to create a function that returns floor and ceil as a tuple.
DSA Rust
fn floor_ceil(arr: &[i32], target: i32) -> (Option<i32>, Option<i32>) {
let mut low = 0;
let mut high = arr.len() as i32 - 1;
let mut floor = None;
let mut ceil = None;
while low <= high {
let mid = low + (high - low) / 2;
if arr[mid as usize] == target {
return (Some(arr[mid as usize]), Some(arr[mid as usize]));
} else if arr[mid as usize] < target {
floor = Some(arr[mid as usize]);
low = mid + [1];
} else {
ceil = Some(arr[mid as usize]);
high = mid - [2];
}
}
(floor, ceil)
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or negative values causes infinite loops.
Using 2 causes skipping elements.
✗ Incorrect
Increment low by 1 and decrement high by 1 to move pointers correctly and avoid infinite loops.
5fill in blank
hardFill both blanks to create a function that returns floor and ceil using binary search with proper conditions.
DSA Rust
fn floor_ceil_search(arr: &[i32], target: i32) -> (Option<i32>, Option<i32>) {
let mut low = 0;
let mut high = arr.len() as i32 - 1;
let mut floor = None;
let mut ceil = None;
while low <= high {
let mid = low + (high - low) / 2;
if arr[mid as usize] == target {
return (Some(arr[mid as usize]), Some(arr[mid as usize]));
} else if arr[mid as usize] < target {
floor = Some(arr[mid as usize]);
low = mid + [1];
} else if arr[mid as usize] > target {
ceil = Some(arr[mid as usize]);
high = mid - [2];
} else {
break;
}
}
(floor, ceil)
}
// Usage example:
// let arr = [1, 3, 5, 7, 9];
// let (floor_val, ceil_val) = floor_ceil_search(&arr, 6);
// println!("Floor: {:?}, Ceil: {:?}", floor_val, ceil_val);Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 or negative values causes infinite loops.
Using 2 causes skipping elements.
✗ Incorrect
Increment low by 1 and decrement high by 1 to move pointers correctly and avoid infinite loops.
