Challenge - 5 Problems
Binary Search Mastery
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 for the value 7 in a sorted array?
DSA Rust
fn binary_search(arr: &[i32], target: i32, low: usize, high: usize) -> Option<usize> {
if low > high {
return None;
}
let mid = low + (high - low) / 2;
if arr[mid] == target {
Some(mid)
} 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, 11];
let result = binary_search(&arr, 7, 0, arr.len() - 1);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Remember that array indices start at 0 and the array is sorted.
✗ Incorrect
The target 7 is at index 3 in the array [1, 3, 5, 7, 9, 11]. The binary search finds it and returns Some(3).
🧠 Conceptual
intermediateUnderstanding Divide and Conquer in Binary Search
Which statement best describes how binary search uses the divide and conquer approach?
Attempts:
2 left
💡 Hint
Think about how the search space changes after each comparison.
✗ Incorrect
Binary search splits the array into two halves and only continues searching the half where the target could be, reducing the search space each time.
🔧 Debug
advancedIdentify the Bug in Binary Search Implementation
What error will occur when running this Rust binary search code and why?
DSA Rust
fn binary_search(arr: &[i32], target: i32, low: usize, high: usize) -> Option<usize> {
if low > high {
return None;
}
let mid = low + (high - low) / 2;
if arr[mid] == target {
Some(mid)
} 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];
let result = binary_search(&arr, 1, 0, arr.len() - 1);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Check the base case condition and what happens when mid is zero.
✗ Incorrect
When mid is 0, mid - 1 causes unsigned integer underflow, leading to panic at runtime.
🚀 Application
advancedBinary Search on a Rotated Sorted Array
Given a rotated sorted array, which approach correctly finds the target using binary search?
Attempts:
2 left
💡 Hint
In a rotated array, one half is always sorted.
✗ Incorrect
By checking which half is sorted, we can decide where the target might be and continue binary search accordingly.
❓ Predict Output
expertOutput of Iterative Binary Search with Edge Cases
What is the output of this Rust code performing iterative binary search for target 15 in the array?
DSA Rust
fn binary_search_iter(arr: &[i32], target: i32) -> Option<usize> {
let mut low = 0;
let mut high = arr.len();
while low < high {
let mid = low + (high - low) / 2;
if arr[mid] == target {
return Some(mid);
} else if arr[mid] < target {
low = mid + 1;
} else {
high = mid;
}
}
None
}
fn main() {
let arr = [5, 10, 15, 20, 25];
let result = binary_search_iter(&arr, 15);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Remember that array indices start at 0 and the target is 15.
✗ Incorrect
The target 15 is at index 2. The iterative binary search returns Some(2).
