Challenge - 5 Problems
Rotated Array Search Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of search in rotated sorted array
What is the output of the following C++ code that searches for a target in a rotated sorted array?
DSA C++
int search(const vector<int>& nums, int target) { int left = 0, right = nums.size() - 1; while (left <= right) { int mid = left + (right - left) / 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; } int main() { vector<int> arr = {4,5,6,7,0,1,2}; int target = 0; cout << search(arr, target); return 0; }
Attempts:
2 left
💡 Hint
Think about where the target 0 is located in the rotated array and how the binary search narrows down the search space.
✗ Incorrect
The target 0 is at index 4 in the rotated sorted array. The binary search correctly finds it and returns 4.
❓ Predict Output
intermediate2:00remaining
Output of search for missing element
What is the output of the following code when searching for a target not present in the rotated sorted array?
DSA C++
int search(const vector<int>& nums, int target) { int left = 0, right = nums.size() - 1; while (left <= right) { int mid = left + (right - left) / 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; } int main() { vector<int> arr = {4,5,6,7,0,1,2}; int target = 3; cout << search(arr, target); return 0; }
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
advanced1:30remaining
Why binary search works on rotated sorted arrays
Why can binary search be applied to a rotated sorted array to find a target efficiently?
Attempts:
2 left
💡 Hint
Think about how the array is split and what property remains after rotation.
✗ 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, enabling efficient binary search.
🔧 Debug
advanced2:00remaining
Identify the bug in rotated array search code
What error will the following code produce when searching in a rotated sorted array?
DSA C++
int search(const vector<int>& nums, int target) { int left = 0, right = nums.size() - 1; while (left < right) { int mid = left + (right - left) / 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; }
Attempts:
2 left
💡 Hint
Check the loop condition and what happens when left equals right.
✗ Incorrect
The loop condition 'left < right' excludes the case when left == right, so if the target is at index right, it may be missed. The correct condition should be 'left <= right'.
🚀 Application
expert3:00remaining
Find minimum element index in rotated sorted array
Given a rotated sorted array with unique elements, which code snippet correctly finds 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 rotated array.
✗ Incorrect
Option A correctly uses binary search to find the minimum by comparing mid with right. If nums[mid] > nums[right], the minimum is in the right half; else in the left half including mid.