Challenge - 5 Problems
Binary Search Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Binary Search Iterative for Present Element
What is the output of the following Rust code that performs an iterative binary search for the value 7 in a sorted array?
DSA Rust
fn binary_search_iterative(arr: &[i32], target: i32) -> i32 {
let mut left = 0;
let mut right = arr.len() as i32 - 1;
while left <= right {
let mid = left + (right - left) / 2;
if arr[mid as usize] == target {
return mid;
} else if arr[mid as usize] < target {
left = mid + 1;
} else {
right = mid - 1;
}
}
-1
}
fn main() {
let arr = [1, 3, 5, 7, 9, 11];
let index = binary_search_iterative(&arr, 7);
println!("{}", index);
}Attempts:
2 left
💡 Hint
Remember that array indices start at 0 and 7 is the fourth element in the array.
✗ Incorrect
The array is [1, 3, 5, 7, 9, 11]. The value 7 is at index 3 (0-based). The binary search finds it and returns index 3.
❓ Predict Output
intermediateOutput of Binary Search Iterative for Missing Element
What is the output of the following Rust code that performs an iterative binary search for the value 8 in a sorted array?
DSA Rust
fn binary_search_iterative(arr: &[i32], target: i32) -> i32 {
let mut left = 0;
let mut right = arr.len() as i32 - 1;
while left <= right {
let mid = left + (right - left) / 2;
if arr[mid as usize] == target {
return mid;
} else if arr[mid as usize] < target {
left = mid + 1;
} else {
right = mid - 1;
}
}
-1
}
fn main() {
let arr = [2, 4, 6, 10, 12];
let index = binary_search_iterative(&arr, 8);
println!("{}", index);
}Attempts:
2 left
💡 Hint
If the target is not found, the function returns -1.
✗ Incorrect
The array is [2, 4, 6, 10, 12]. The value 8 is not in the array. The function returns -1 to indicate not found.
🧠 Conceptual
advancedWhy use mid = left + (right - left) / 2 in Binary Search?
In the iterative binary search, why is the middle index calculated as
mid = left + (right - left) / 2 instead of mid = (left + right) / 2?Attempts:
2 left
💡 Hint
Think about what happens if left and right are very large numbers.
✗ Incorrect
Using left + (right - left) / 2 avoids potential overflow when left + right exceeds the maximum integer value.
🔧 Debug
advancedIdentify the Bug in Binary Search Iterative Code
What error will occur when running this Rust code for binary search?
DSA Rust
fn binary_search_iterative(arr: &[i32], target: i32) -> i32 {
let mut left = 0;
let mut right = arr.len();
while left <= right {
let mid = left + (right - left) / 2;
if arr[mid] == target {
return mid as i32;
} else if arr[mid] < target {
left = mid + 1;
} else {
right = mid - 1;
}
}
-1
}
fn main() {
let arr = [1, 2, 3, 4, 5];
let index = binary_search_iterative(&arr, 3);
println!("{}", index);
}Attempts:
2 left
💡 Hint
Check how right is initialized and used as an index.
✗ Incorrect
The variable right is initialized to arr.len(), which is one past the last valid index. Accessing arr[mid] can cause an index out of bounds panic.
🚀 Application
expertNumber of Iterations in Binary Search
Given a sorted array of length 64, what is the maximum number of iterations the iterative binary search algorithm will perform to find a target or conclude it is not present?
Attempts:
2 left
💡 Hint
Binary search halves the search space each iteration. Think about powers of 2.
✗ Incorrect
Binary search reduces the search space by half each iteration. For 64 elements, maximum iterations = log2(64) + 1 = 6 + 1 = 7.
