4. Consider this Rust function to find floor in a sorted array:
fn find_floor(arr: &[i32], target: i32) -> Option<i32> {
let mut low = 0;
let mut high = arr.len() - 1;
let mut floor = None;
while low <= high {
let mid = (low + high) / 2;
if arr[mid] == target {
return Some(arr[mid]);
} else if arr[mid] < target {
floor = Some(arr[mid]);
low = mid + 1;
} else {
high = mid - 1;
}
}
floor
}
What is the bug in this code?
medium
A. Incorrect update of low and high pointers
B. Using arr.len() - 1 without checking if array is empty
C. Returning Some(arr[mid]) instead of index
D. Not handling the case when target is smaller than all elements
Solution
Step 1: Analyze initialization of high pointer
Using arr.len() - 1 causes panic if array is empty because len() is 0 and subtracting 1 underflows.
Step 2: Check other parts of code
Other logic for binary search and floor update is correct. The main bug is unsafe initialization.
Final Answer:
Using arr.len() - 1 without checking if array is empty -> Option B
Quick Check:
Empty array causes panic at arr.len() - 1 [OK]
Hint: Check empty array before subtracting 1 from length [OK]
Common Mistakes:
Ignoring empty array edge case
Assuming array always has elements
Confusing return value with index
5. You have a sorted array arr = [1, 3, 5, 7, 9]. You want to find the floor and ceil for multiple targets: [0, 4, 10]. Which Rust code snippet correctly returns a vector of tuples with floor and ceil for each target? Assume floor or ceil is None if not found.
hard
A. ```rust
fn floor_ceil(arr: &[i32], targets: &[i32]) -> Vec<(Option<i32>, Option<i32>)> {
targets.iter().map(|&t| {
let floor = arr.iter().filter(|&&x| x <= t).min().copied();
let ceil = arr.iter().filter(|&&x| x >= t).max().copied();
(floor, ceil)
}).collect()
}
```
B. ```rust
fn floor_ceil(arr: &[i32], targets: &[i32]) -> Vec<(Option<i32>, Option<i32>)> {
targets.iter().map(|&t| {
let floor = arr.iter().filter(|&&x| x < t).max().copied();
let ceil = arr.iter().filter(|&&x| x > t).min().copied();
(floor, ceil)
}).collect()
}
```
C. ```rust
fn floor_ceil(arr: &[i32], targets: &[i32]) -> Vec<(Option<i32>, Option<i32>)> {
targets.iter().map(|&t| {
let floor = arr.iter().find(|&&x| x <= t).copied();
let ceil = arr.iter().find(|&&x| x >= t).copied();
(floor, ceil)
}).collect()
}
```
D. ```rust
fn floor_ceil(arr: &[i32], targets: &[i32]) -> Vec<(Option<i32>, Option<i32>)> {
targets.iter().map(|&t| {
let floor = arr.iter().filter(|&&x| x <= t).max().copied();
let ceil = arr.iter().filter(|&&x| x >= t).min().copied();
(floor, ceil)
}).collect()
}
```
Solution
Step 1: Understand floor and ceil conditions
Floor is max element ≤ target, ceil is min element ≥ target.
Step 2: Analyze each code snippet
```rust
fn floor_ceil(arr: &[i32], targets: &[i32]) -> Vec<(Option<i32>, Option<i32>)> {
targets.iter().map(|&t| {
let floor = arr.iter().filter(|&&x| x <= t).max().copied();
let ceil = arr.iter().filter(|&&x| x >= t).min().copied();
(floor, ceil)
}).collect()
}
``` uses filter with ≤ and ≥, then max and min respectively, which matches floor and ceil definitions. ```rust
fn floor_ceil(arr: &[i32], targets: &[i32]) -> Vec<(Option<i32>, Option<i32>)> {
targets.iter().map(|&t| {
let floor = arr.iter().filter(|&&x| x < t).max().copied();
let ceil = arr.iter().filter(|&&x| x > t).min().copied();
(floor, ceil)
}).collect()
}
``` uses < and > which exclude equality. ```rust
fn floor_ceil(arr: &[i32], targets: &[i32]) -> Vec<(Option<i32>, Option<i32>)> {
targets.iter().map(|&t| {
let floor = arr.iter().find(|&&x| x <= t).copied();
let ceil = arr.iter().find(|&&x| x >= t).copied();
(floor, ceil)
}).collect()
}
``` uses find which returns first match, not max or min. ```rust
fn floor_ceil(arr: &[i32], targets: &[i32]) -> Vec<(Option<i32>, Option<i32>)> {
targets.iter().map(|&t| {
let floor = arr.iter().filter(|&&x| x <= t).min().copied();
let ceil = arr.iter().filter(|&&x| x >= t).max().copied();
(floor, ceil)
}).collect()
}
``` reverses min and max causing wrong results.
Final Answer:
filter(|&&x| x <= t).max() for floor, filter(|&&x| x >= t).min() for ceil -> Option D
Quick Check:
Floor = max ≤ target, Ceil = min ≥ target [OK]
Hint: Floor uses max ≤ target, ceil uses min ≥ target filters [OK]
Common Mistakes:
Using strict < or > instead of ≤ or ≥
Using find instead of max/min for floor/ceil
Swapping min and max in floor/ceil logic
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.