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 C code that searches for a target in a rotated sorted array?
DSA C
int search(int* nums, int numsSize, int target) { int left = 0, right = numsSize - 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() { int arr[] = {4,5,6,7,0,1,2}; int target = 0; int index = search(arr, 7, target); printf("%d\n", index); return 0; }
Attempts:
2 left
💡 Hint
Think about how the binary search adjusts the search range in a rotated array.
✗ Incorrect
The target 0 is at index 4 in the rotated sorted array. The search function correctly finds and returns this index.
❓ Predict Output
intermediateResult of searching missing element
What is the output of this code when searching for a target not in the rotated sorted array?
DSA C
int search(int* nums, int numsSize, int target) { int left = 0, right = numsSize - 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() { int arr[] = {4,5,6,7,0,1,2}; int target = 3; int index = search(arr, 7, target); printf("%d\n", index); return 0; }
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
advancedUnderstanding the search logic in rotated sorted array
In the search algorithm for a rotated sorted array, why do we check if nums[left] <= nums[mid]?
Attempts:
2 left
💡 Hint
Think about how the array is split into sorted and unsorted halves.
✗ Incorrect
Checking nums[left] <= nums[mid] helps identify if the left half is sorted, which guides the search direction.
❓ Predict Output
advancedOutput of search with duplicate elements
What is the output of this code searching for target 2 in a rotated sorted array with duplicates?
DSA C
int search(int* nums, int numsSize, int target) { int left = 0, right = numsSize - 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() { int arr[] = {2,3,4,2,2,2}; int target = 2; int index = search(arr, 6, target); printf("%d\n", index); return 0; }
Attempts:
2 left
💡 Hint
The function returns the first found index of the target.
✗ Incorrect
The target 2 is found at index 0 first during the search, so the function returns 0.
🔧 Debug
expertIdentify the error in this rotated array search code
What error will this code produce when searching in a rotated sorted array?
DSA C
int search(int* nums, int numsSize, int target) { int left = 0, right = numsSize - 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; } } if (nums[left] == target) return left; return -1; } int main() { int arr[] = {4,5,6,7,0,1,2}; int target = 0; int index = search(arr, 7, target); printf("%d\n", index); return 0; }
Attempts:
2 left
💡 Hint
Check the loop condition and how left and right pointers move.
✗ Incorrect
Using 'while (left < right)' can cause the loop to miss checking the last element or get stuck, leading to infinite loop or wrong result.
