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 code that searches for a target in a rotated sorted array?
DSA Python
def search(nums, target): left, right = 0, len(nums) - 1 while left <= right: mid = (left + right) // 2 if nums[mid] == target: return mid if nums[left] <= nums[mid]: if nums[left] <= target < nums[mid]: right = mid - 1 else: left = mid + 1 else: if nums[mid] < target <= nums[right]: left = mid + 1 else: right = mid - 1 return -1 nums = [6,7,8,1,2,3,4,5] target = 3 print(search(nums, target))
Attempts:
2 left
💡 Hint
Trace the binary search steps considering the rotated sorted array halves.
✗ Incorrect
The function uses binary search adapted for rotated sorted arrays. The target 3 is at index 5 in the array.
❓ Predict Output
intermediateFind index of target not present
What does the function return when the target is not in the rotated sorted array?
DSA Python
def search(nums, target): left, right = 0, len(nums) - 1 while left <= right: mid = (left + right) // 2 if nums[mid] == target: return mid if nums[left] <= nums[mid]: if nums[left] <= target < nums[mid]: right = mid - 1 else: left = mid + 1 else: if nums[mid] < target <= nums[right]: left = mid + 1 else: right = mid - 1 return -1 nums = [4,5,6,7,0,1,2] target = 3 print(search(nums, target))
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.
🔧 Debug
advancedIdentify the error in this rotated array search code
What error does this code produce when searching for target 2?
DSA Python
def search(nums, target): left, right = 0, len(nums) - 1 while left < right: mid = (left + right) // 2 if nums[mid] == target: return mid if nums[left] <= nums[mid]: if nums[left] <= target < nums[mid]: right = mid - 1 else: left = mid + 1 else: if nums[mid] < target <= nums[right]: left = mid + 1 else: right = mid - 1 return -1 nums = [3,4,5,1,2] target = 2 print(search(nums, target))
Attempts:
2 left
💡 Hint
Check the loop condition and how it affects the search range.
✗ Incorrect
The loop condition uses < instead of <=, so it may miss the correct index and return -1.
❓ Predict Output
advancedOutput of search with duplicates in rotated array
What is the output of the code searching for target 2 in a rotated array with duplicates?
DSA Python
def search(nums, target): left, right = 0, len(nums) - 1 while left <= right: mid = (left + right) // 2 if nums[mid] == target: return mid if nums[left] <= nums[mid]: if nums[left] <= target < nums[mid]: right = mid - 1 else: left = mid + 1 else: if nums[mid] < target <= nums[right]: left = mid + 1 else: right = mid - 1 return -1 nums = [2,3,4,2,2,2] target = 2 print(search(nums, target))
Attempts:
2 left
💡 Hint
The function returns the first found index of the target.
✗ Incorrect
The target 2 appears multiple times; the function returns the first found index 0.
🧠 Conceptual
expertWhy binary search works on rotated sorted arrays
Which statement best explains why binary search can be adapted to work on rotated sorted arrays?
Attempts:
2 left
💡 Hint
Think about how the array is split and what properties remain after rotation.
✗ Incorrect
In a rotated sorted array, one half is always sorted, which helps decide where to continue searching.
