Challenge - 5 Problems
Rotated Array 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 Go code that searches for a target in a rotated sorted array?
DSA Go
package main import "fmt" func search(nums []int, target int) int { left, right := 0, len(nums)-1 for left <= right { mid := (left + right) / 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 } func main() { nums := []int{4,5,6,7,0,1,2} target := 0 fmt.Println(search(nums, target)) }
Attempts:
2 left
💡 Hint
Remember the array is rotated; the target 0 is not at the start.
✗ Incorrect
The target 0 is at index 4 in the rotated array [4,5,6,7,0,1,2]. The binary search correctly finds it and returns 4.
❓ Predict Output
intermediate2:00remaining
Output when target is not in rotated array
What is the output of this Go code searching for a target not present in the rotated sorted array?
DSA Go
package main import "fmt" func search(nums []int, target int) int { left, right := 0, len(nums)-1 for left <= right { mid := (left + right) / 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 } func main() { nums := []int{4,5,6,7,0,1,2} target := 3 fmt.Println(search(nums, target)) }
Attempts:
2 left
💡 Hint
Target 3 is not in the array.
✗ Incorrect
Since 3 is not in the array, the function returns -1 indicating not found.
🔧 Debug
advanced2:00remaining
Identify the error in rotated array search code
What error will this Go code produce when searching in a rotated sorted array?
DSA Go
package main import "fmt" func search(nums []int, target int) int { left, right := 0, len(nums)-1 for left < right { mid := (left + right) / 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 } func main() { nums := []int{4,5,6,7,0,1,2} target := 0 fmt.Println(search(nums, target)) }
Attempts:
2 left
💡 Hint
Check the loop condition and boundary checks carefully.
✗ Incorrect
The loop condition 'left < right' misses the case when left == right, causing the target at that position to be missed sometimes, leading to incorrect -1 return.
🧠 Conceptual
advanced1:30remaining
Why binary search works on rotated sorted arrays
Why can binary search be adapted to find an element in a rotated sorted array?
Attempts:
2 left
💡 Hint
Think about how the array is split after rotation.
✗ Incorrect
In a rotated sorted array, at least one half (left or right) is sorted. This property allows binary search to decide which half to discard, maintaining O(log n) time.
🚀 Application
expert2:30remaining
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 smaller than its previous element in the rotated array.
✗ Incorrect
Option B correctly uses binary search to find the smallest element by comparing mid with right, narrowing the search space until left points to the minimum.