Challenge - 5 Problems
Rotated Array Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of search in rotated sorted array
What is the output of the following Rust code that searches for a target in a rotated sorted array?
DSA Rust
fn search(nums: Vec<i32>, target: i32) -> i32 {
let mut left = 0;
let mut right = nums.len() as i32 - 1;
while left <= right {
let mid = left + (right - left) / 2;
if nums[mid as usize] == target {
return mid;
}
if nums[left as usize] <= nums[mid as usize] {
if nums[left as usize] <= target && target < nums[mid as usize] {
right = mid - 1;
} else {
left = mid + 1;
}
} else {
if nums[mid as usize] < target && target <= nums[right as usize] {
left = mid + 1;
} else {
right = mid - 1;
}
}
}
-1
}
fn main() {
let nums = vec![4,5,6,7,0,1,2];
let target = 0;
println!("{}", search(nums, target));
}Attempts:
2 left
💡 Hint
Think about how the binary search adjusts left and right pointers in a rotated array.
✗ Incorrect
The function returns the index of the target 0 in the rotated sorted array. The target 0 is at index 4.
❓ Predict Output
intermediateOutput when target is not present
What is the output of this Rust code searching for a target not in the rotated sorted array?
DSA Rust
fn search(nums: Vec<i32>, target: i32) -> i32 {
let mut left = 0;
let mut right = nums.len() as i32 - 1;
while left <= right {
let mid = left + (right - left) / 2;
if nums[mid as usize] == target {
return mid;
}
if nums[left as usize] <= nums[mid as usize] {
if nums[left as usize] <= target && target < nums[mid as usize] {
right = mid - 1;
} else {
left = mid + 1;
}
} else {
if nums[mid as usize] < target && target <= nums[right as usize] {
left = mid + 1;
} else {
right = mid - 1;
}
}
}
-1
}
fn main() {
let nums = vec![4,5,6,7,0,1,2];
let target = 3;
println!("{}", search(nums, target));
}Attempts:
2 left
💡 Hint
If the target is not found, the function returns -1.
✗ Incorrect
The target 3 is not in the array, so the function returns -1.
🧠 Conceptual
advancedWhy binary search works on rotated sorted arrays
Why can binary search be applied to a rotated sorted array?
Attempts:
2 left
💡 Hint
Think about the property of the rotated array halves.
✗ Incorrect
In a rotated sorted array, one half is always sorted. This helps decide which half to search next using binary search.
🔧 Debug
advancedIdentify the error in this rotated array search code
What error will this Rust code produce when searching a rotated sorted array?
DSA Rust
fn search(nums: Vec<i32>, target: i32) -> i32 {
let mut left = 0;
let mut right = nums.len() - 1;
while left <= right {
let mid = (left + right) / 2;
if nums[mid] == target {
return mid as i32;
}
if nums[left] <= nums[mid] {
if nums[left] <= target && target < nums[mid] {
right = mid - 1;
} else {
left = mid + 1;
}
} else {
if nums[mid] < target && target <= nums[right] {
left = mid + 1;
} else {
right = mid - 1;
}
}
}
-1
}
fn main() {
let nums = vec![4,5,6,7,0,1,2];
let target = 0;
println!("{}", search(nums, target));
}Attempts:
2 left
💡 Hint
Check how right is declared and updated.
✗ Incorrect
The variable right is usize (unsigned). When mid is 0 and right = mid - 1, it underflows causing panic.
🚀 Application
expertFind minimum element index in rotated sorted array
Given a rotated sorted array with no duplicates, which Rust code correctly returns the index of the minimum element?
Attempts:
2 left
💡 Hint
The minimum element is the only element where the previous element is greater or it is the smallest in the right half.
✗ Incorrect
Option A correctly uses binary search to find the minimum by comparing mid and right elements, narrowing the search space until left == right.
