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 Java code that searches for a target in a rotated sorted array?
DSA Java
public class Solution { public static int search(int[] nums, int target) { int left = 0, right = nums.length - 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; } public static void main(String[] args) { int[] nums = {4,5,6,7,0,1,2}; System.out.println(search(nums, 0)); } }
Attempts:
2 left
💡 Hint
Think about how the array is rotated and where the target 0 is located.
✗ Incorrect
The target 0 is at index 4 in the rotated array. The binary search correctly finds it and returns 4.
❓ Predict Output
intermediateOutput when target is not present
What will be the output of the following code when searching for target 3 in the rotated sorted array?
DSA Java
public class Solution { public static int search(int[] nums, int target) { int left = 0, right = nums.length - 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; } public static void main(String[] args) { int[] nums = {4,5,6,7,0,1,2}; System.out.println(search(nums, 3)); } }
Attempts:
2 left
💡 Hint
Check if the target 3 exists in the array.
✗ Incorrect
The target 3 is not present in the array, so the function returns -1.
🔧 Debug
advancedIdentify the error in the search method
What error will occur when running this code to search in a rotated sorted array?
DSA Java
public class Solution { public static int search(int[] nums, int target) { int left = 0, right = nums.length - 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; } }
Attempts:
2 left
💡 Hint
Check the loop condition and how left and right pointers move.
✗ Incorrect
The loop condition 'left < right' fails to check the element when left == right, causing the function to return -1 even when the target is present at that position.
🧠 Conceptual
advancedWhy does binary search work 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 during the search.
✗ Incorrect
In a rotated sorted array, one half is always sorted, so we can check which half to continue searching.
❓ Predict Output
expertOutput of search with duplicates in rotated array
What is the output of the following code searching for target 2 in a rotated sorted array with duplicates?
DSA Java
public class Solution { public static int search(int[] nums, int target) { int left = 0, right = nums.length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (nums[mid] == target) return mid; if (nums[left] == nums[mid] && nums[mid] == nums[right]) { left++; right--; } else 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; } public static void main(String[] args) { int[] nums = {2,2,3,4,2,2}; System.out.println(search(nums, 2)); } }
Attempts:
2 left
💡 Hint
Duplicates can cause multiple valid indices; the code returns the first found.
✗ Incorrect
The code returns index 0 because it finds the target 2 at the first occurrence during the search.
