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 the target 3 in a rotated sorted array?
DSA C#
using System; class Program { 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; } static void Main() { int[] arr = {4,5,6,7,0,1,2}; Console.WriteLine(Search(arr, 3)); } }
Attempts:
2 left
💡 Hint
Think about whether the target 3 exists in the array.
✗ Incorrect
The array is rotated and does not contain the number 3. The search returns -1 when the target is not found.
❓ Predict Output
intermediateFind index of target in rotated sorted array
What index does the following code return when searching for target 0 in the rotated sorted array?
DSA C#
using System; class Program { 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; } static void Main() { int[] arr = {4,5,6,7,0,1,2}; Console.WriteLine(Search(arr, 0)); } }
Attempts:
2 left
💡 Hint
Check the position of 0 in the array.
✗ Incorrect
The target 0 is at index 4 in the array.
🔧 Debug
advancedIdentify the error in rotated array search code
What error does the following code produce when searching for target 5 in the rotated sorted array?
DSA C#
using System; class Program { 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; } static void Main() { int[] arr = {6,7,0,1,2,4,5}; Console.WriteLine(Search(arr, 5)); } }
Attempts:
2 left
💡 Hint
Check the loop condition and how it affects the search.
✗ Incorrect
The loop uses 'left < right' which can skip checking the last element, causing the function to miss the target and return -1.
❓ Predict Output
advancedOutput of search for target 1 in rotated array
What is the output of the following code searching for target 1 in the rotated sorted array?
DSA C#
using System; class Program { 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; } static void Main() { int[] arr = {5,6,7,8,9,1,2,3,4}; Console.WriteLine(Search(arr, 1)); } }
Attempts:
2 left
💡 Hint
Locate the index of 1 in the array.
✗ Incorrect
The target 1 is at index 5 in the array.
🧠 Conceptual
expertMinimum number of comparisons in rotated sorted array search
What is the minimum number of comparisons needed in the worst case to find a target in a rotated sorted array of size n using binary search?
Attempts:
2 left
💡 Hint
Think about how binary search divides the search space.
✗ Incorrect
Binary search on a rotated sorted array still divides the search space in half each step, so it takes O(log n) comparisons in the worst case.
