Challenge - 5 Problems
Binary Search Recursive Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Recursive Binary Search Call
What is the output of the following Rust code that performs a recursive binary search on a sorted array?
DSA Rust
fn binary_search(arr: &[i32], target: i32, low: usize, high: usize) -> i32 {
if low > high {
return -1;
}
let mid = low + (high - low) / 2;
if arr[mid] == target {
mid as i32
} else if arr[mid] > target {
binary_search(arr, target, low, mid - 1)
} else {
binary_search(arr, target, mid + 1, high)
}
}
fn main() {
let arr = [2, 4, 6, 8, 10, 12];
let result = binary_search(&arr, 10, 0, arr.len() - 1);
println!("{}", result);
}Attempts:
2 left
💡 Hint
Remember that array indices start at 0 and the function returns the index of the target if found.
✗ Incorrect
The target 10 is at index 4 in the array [2, 4, 6, 8, 10, 12]. The recursive binary search returns this index.
❓ Predict Output
intermediateResult of Recursive Binary Search for Missing Element
What will be printed when searching for a value not present in the array using this recursive binary search?
DSA Rust
fn binary_search(arr: &[i32], target: i32, low: usize, high: usize) -> i32 {
if low > high {
return -1;
}
let mid = low + (high - low) / 2;
if arr[mid] == target {
mid as i32
} else if arr[mid] > target {
binary_search(arr, target, low, mid - 1)
} else {
binary_search(arr, target, mid + 1, high)
}
}
fn main() {
let arr = [1, 3, 5, 7, 9];
let result = binary_search(&arr, 4, 0, arr.len() - 1);
println!("{}", result);
}Attempts:
2 left
💡 Hint
If the target is not found, the function returns -1.
✗ Incorrect
Since 4 is not in the array, the function returns -1 indicating the target is missing.
🔧 Debug
advancedIdentify the Runtime Error in Recursive Binary Search
This Rust code for recursive binary search sometimes causes a runtime error. What is the cause?
DSA Rust
fn binary_search(arr: &[i32], target: i32, low: usize, high: usize) -> i32 {
if low > high {
return -1;
}
let mid = (low + high) / 2;
if arr[mid] == target {
mid as i32
} else if arr[mid] > target {
binary_search(arr, target, low, mid - 1)
} else {
binary_search(arr, target, mid + 1, high)
}
}
fn main() {
let arr = [1, 2, 3, 4, 5];
let result = binary_search(&arr, 1, 0, arr.len() - 1);
println!("{}", result);
}Attempts:
2 left
💡 Hint
Check what happens when mid is zero and you subtract one.
✗ Incorrect
When mid is 0, mid - 1 causes underflow because usize cannot be negative, leading to a large number and out-of-bounds access.
🧠 Conceptual
advancedWhy Use mid = low + (high - low) / 2 in Binary Search?
In recursive binary search, why is the middle index calculated as
mid = low + (high - low) / 2 instead of mid = (low + high) / 2?Attempts:
2 left
💡 Hint
Think about what happens if low and high are very large numbers.
✗ Incorrect
Adding low and high directly can overflow the integer limit, but subtracting first avoids this problem.
❓ Predict Output
expertOutput of Recursive Binary Search with Duplicate Elements
What is the output of this recursive binary search when searching for 3 in an array with duplicates?
DSA Rust
fn binary_search(arr: &[i32], target: i32, low: usize, high: usize) -> i32 {
if low > high {
return -1;
}
let mid = low + (high - low) / 2;
if arr[mid] == target {
mid as i32
} else if arr[mid] > target {
binary_search(arr, target, low, mid - 1)
} else {
binary_search(arr, target, mid + 1, high)
}
}
fn main() {
let arr = [1, 2, 3, 3, 3, 4, 5];
let result = binary_search(&arr, 3, 0, arr.len() - 1);
println!("{}", result);
}Attempts:
2 left
💡 Hint
The function returns the first found index of the target, which depends on mid calculation.
✗ Incorrect
The mid calculation leads to index 3 being found first, which contains 3.
