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 JavaScript code that searches for a target in a rotated sorted array?
DSA Javascript
function search(nums, target) {
let left = 0, right = nums.length - 1;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (nums[mid] === target) return mid;
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;
}
}
}
return -1;
}
console.log(search([4,5,6,7,0,1,2], 0));Attempts:
2 left
💡 Hint
Think about how the rotated sorted array is split and how binary search adapts.
✗ Incorrect
The target 0 is at index 4 in the rotated array [4,5,6,7,0,1,2]. The search function correctly finds and returns index 4.
❓ Predict Output
intermediateOutput when target is not in rotated sorted array
What is the output of this code when the target is not present in the rotated sorted array?
DSA Javascript
function search(nums, target) {
let left = 0, right = nums.length - 1;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (nums[mid] === target) return mid;
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;
}
}
}
return -1;
}
console.log(search([4,5,6,7,0,1,2], 3));Attempts:
2 left
💡 Hint
If the target is not found, what does the function return?
✗ Incorrect
The target 3 is not in the array, so the function returns -1 indicating not found.
🧠 Conceptual
advancedWhy binary search works on rotated sorted arrays
Why can binary search be adapted to work on a rotated sorted array?
Attempts:
2 left
💡 Hint
Think about how the array is split by the rotation point.
✗ Incorrect
In a rotated sorted array, one half is always sorted. This property lets us decide which half to discard and which to keep searching, adapting binary search.
🔧 Debug
advancedIdentify the error in this rotated array search code
What error will this code produce when searching in a rotated sorted array?
DSA Javascript
function search(nums, target) {
let left = 0, right = nums.length - 1;
while (left < right) {
let mid = Math.floor((left + right) / 2);
if (nums[mid] === target) return mid;
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;
}
}
}
return -1;
}
console.log(search([4,5,6,7,0,1,2], 0));Attempts:
2 left
💡 Hint
Check the loop condition and how it affects the search range.
✗ Incorrect
Using 'while (left < right)' misses the case when left equals right, so the target at that position is never checked, causing a wrong -1 return.
🚀 Application
expertFind minimum element in rotated sorted array
Given a rotated sorted array with no duplicates, which code correctly finds the minimum element?
Attempts:
2 left
💡 Hint
Use binary search logic to find the point where order breaks.
✗ Incorrect
Option B uses binary search to find the minimum by comparing mid and right elements, narrowing the search space efficiently.
