Challenge - 5 Problems
Rotated Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Find minimum element output after rotation
What is the output of the following C++ code that finds the minimum element in a rotated sorted array?
DSA C++
int findMin(vector<int>& nums) { int left = 0, right = nums.size() - 1; while (left < right) { int mid = left + (right - left) / 2; if (nums[mid] > nums[right]) { left = mid + 1; } else { right = mid; } } return nums[left]; } int main() { vector<int> nums = {4,5,6,7,0,1,2}; cout << findMin(nums) << endl; return 0; }
Attempts:
2 left
💡 Hint
Think about where the smallest element lies in a rotated sorted array.
✗ Incorrect
The code uses binary search to find the minimum element. Since the array is rotated, the minimum is the only element smaller than the last element in the array segment. The code narrows down to index 4 where the value is 0.
❓ Predict Output
intermediate2:00remaining
Minimum element in a rotated array with duplicates
What will be the output of this code when the input array contains duplicates?
DSA C++
int findMin(vector<int>& nums) { int left = 0, right = nums.size() - 1; while (left < right) { int mid = left + (right - left) / 2; if (nums[mid] > nums[right]) { left = mid + 1; } else if (nums[mid] < nums[right]) { right = mid; } else { right--; } } return nums[left]; } int main() { vector<int> nums = {2,2,2,0,1,2}; cout << findMin(nums) << endl; return 0; }
Attempts:
2 left
💡 Hint
Duplicates require a slight modification in the binary search approach.
✗ Incorrect
When duplicates exist, if nums[mid] equals nums[right], we reduce right by one to skip duplicates. The minimum element here is 0 at index 3.
🔧 Debug
advanced2:00remaining
Identify the error in this minimum finder code
What error will this code produce when trying to find the minimum in a rotated sorted array?
DSA C++
int findMin(vector<int>& nums) { int left = 0, right = nums.size() - 1; while (left <= right) { int mid = left + (right - left) / 2; if (nums[mid] > nums[right]) { left = mid + 1; } else { right = mid - 1; } } return nums[left]; } int main() { vector<int> nums = {3,4,5,1,2}; cout << findMin(nums) << endl; return 0; }
Attempts:
2 left
💡 Hint
Check the loop condition and how left and right pointers move.
✗ Incorrect
The loop condition allows left to go beyond right, and right is set to mid - 1 which can cause left to exceed array bounds. Accessing nums[left] after loop can cause out_of_range error.
❓ Predict Output
advanced2:00remaining
Output of minimum finder on a non-rotated sorted array
What is the output of this code when the input array is sorted but not rotated?
DSA C++
int findMin(vector<int>& nums) { int left = 0, right = nums.size() - 1; while (left < right) { int mid = left + (right - left) / 2; if (nums[mid] > nums[right]) { left = mid + 1; } else { right = mid; } } return nums[left]; } int main() { vector<int> nums = {1,2,3,4,5}; cout << findMin(nums) << endl; return 0; }
Attempts:
2 left
💡 Hint
If the array is not rotated, the minimum is the first element.
✗ Incorrect
The code correctly returns the first element 1 as the minimum in a sorted array without rotation.
🧠 Conceptual
expert2:00remaining
Why binary search works for finding minimum in rotated sorted array
Which statement best explains why binary search can be used to find the minimum element in a rotated sorted array?
Attempts:
2 left
💡 Hint
Think about how rotation affects sorted order and how binary search uses order.
✗ Incorrect
The rotated array consists of two sorted subarrays. Binary search compares middle and right elements to decide which half contains the minimum, discarding the other half each step.