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 the following C code that finds the minimum element in a rotated sorted array?
DSA C
int findMin(int* nums, int numsSize) { int left = 0, right = numsSize - 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() { int arr[] = {4,5,6,7,0,1,2}; int size = sizeof(arr) / sizeof(arr[0]); int min = findMin(arr, size); printf("%d\n", min); 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 its previous. The algorithm narrows down the search space until left meets right at the minimum element, which is 0 here.
❓ Predict Output
intermediateOutput for single element array
What will be the output of the code when the input array has only one element?
DSA C
int findMin(int* nums, int numsSize) { int left = 0, right = numsSize - 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() { int arr[] = {10}; int size = sizeof(arr) / sizeof(arr[0]); int min = findMin(arr, size); printf("%d\n", min); return 0; }
Attempts:
2 left
💡 Hint
Consider the array size and how the loop behaves.
✗ Incorrect
With only one element, the loop condition left < right is false initially, so the function returns the only element, 10.
🔧 Debug
advancedIdentify the error in this modified code
What error will the following code produce when run?
DSA C
int findMin(int* nums, int numsSize) { int left = 0, right = numsSize - 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() { int arr[] = {}; int size = sizeof(arr) / sizeof(arr[0]); int min = findMin(arr, size); printf("%d\n", min); return 0; }
Attempts:
2 left
💡 Hint
Check how the pointers move and the loop condition.
✗ Incorrect
The loop condition and pointer updates cause left to go beyond array bounds, leading to out-of-bounds access when returning nums[left].
🧠 Conceptual
advancedWhy binary search works on rotated sorted arrays
Why does the binary search approach work to find the minimum element in a rotated sorted array?
Attempts:
2 left
💡 Hint
Think about how the rotation affects the sorted order.
✗ Incorrect
The rotated array is partially sorted. By comparing the middle element with the rightmost element, we can decide which half contains the minimum and discard the other half, reducing the search space.
❓ Predict Output
expertOutput for array with duplicates
What is the output of the following code when the input array contains duplicates?
DSA C
int findMin(int* nums, int numsSize) { int left = 0, right = numsSize - 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() { int arr[] = {2,2,2,0,1,2}; int size = sizeof(arr) / sizeof(arr[0]); int min = findMin(arr, size); printf("%d\n", min); return 0; }
Attempts:
2 left
💡 Hint
Duplicates require a slight modification in the binary search approach.
✗ Incorrect
The code handles duplicates by decrementing right when nums[mid] equals nums[right], ensuring the minimum is found correctly. The minimum element is 0.
