Challenge - 5 Problems
Rotated Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateFind the minimum element output
What is the output of this Rust code that finds the minimum element in a rotated sorted array?
DSA Rust
fn find_min(nums: Vec<i32>) -> i32 {
let mut left = 0;
let mut right = nums.len() - 1;
while left < right {
let mid = left + (right - left) / 2;
if nums[mid] > nums[right] {
left = mid + 1;
} else {
right = mid;
}
}
nums[left]
}
fn main() {
let nums = vec![4,5,6,7,0,1,2];
println!("{}", find_min(nums));
}Attempts:
2 left
💡 Hint
Think about how the binary search narrows down the smallest element in a rotated array.
✗ Incorrect
The code uses binary search to find the smallest element. Since the array is rotated, the smallest element is the pivot point where the order breaks. The code compares middle and right elements to decide which half to search next. The output is 0, the smallest element.
❓ Predict Output
intermediateOutput for single element array
What will be printed when running this Rust code with a single-element rotated array?
DSA Rust
fn find_min(nums: Vec<i32>) -> i32 {
let mut left = 0;
let mut right = nums.len() - 1;
while left < right {
let mid = left + (right - left) / 2;
if nums[mid] > nums[right] {
left = mid + 1;
} else {
right = mid;
}
}
nums[left]
}
fn main() {
let nums = vec![10];
println!("{}", find_min(nums));
}Attempts:
2 left
💡 Hint
Consider what happens when the array has only one element.
✗ Incorrect
With one element, the function returns that element immediately without entering the loop. So the output is 10.
❓ Predict Output
advancedOutput with no rotation (sorted array)
What is the output of this Rust code when the input array is sorted but not rotated?
DSA Rust
fn find_min(nums: Vec<i32>) -> i32 {
let mut left = 0;
let mut right = nums.len() - 1;
while left < right {
let mid = left + (right - left) / 2;
if nums[mid] > nums[right] {
left = mid + 1;
} else {
right = mid;
}
}
nums[left]
}
fn main() {
let nums = vec![1,2,3,4,5,6,7];
println!("{}", find_min(nums));
}Attempts:
2 left
💡 Hint
If the array is not rotated, the smallest element is the first one.
✗ Incorrect
Since the array is sorted and not rotated, the smallest element is the first element, 1.
🔧 Debug
advancedIdentify the error in this Rust code
What error will this Rust code produce when trying to find the minimum in a rotated sorted array?
DSA Rust
fn find_min(nums: Vec<i32>) -> i32 {
let mut left = 0;
let mut right = nums.len();
while left < right {
let mid = left + (right - left) / 2;
if nums[mid] > nums[right] {
left = mid + 1;
} else {
right = mid;
}
}
nums[left]
}
fn main() {
let nums = vec![3,4,5,1,2];
println!("{}", find_min(nums));
}Attempts:
2 left
💡 Hint
Check the value of right initialization and how it is used as an index.
✗ Incorrect
The code sets right to nums.len(), which is out of bounds for indexing. Accessing nums[right] causes a panic.
🧠 Conceptual
expertWhy binary search works for finding minimum in rotated sorted array
Why does binary search correctly find the minimum element in a rotated sorted array?
Attempts:
2 left
💡 Hint
Think about how rotation affects sorted order and how comparisons guide the search.
✗ Incorrect
The rotated array is sorted in two parts. Comparing middle and right elements tells which part contains the minimum, allowing binary search to discard half the array each step.
