Challenge - 5 Problems
Rotated Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateFind minimum element output in rotated array
What is the output of the following C# code that finds the minimum element in a rotated sorted array?
DSA C#
int FindMin(int[] nums) { int left = 0, right = nums.Length - 1; while (left < right) { int mid = left + (right - left) / 2; if (nums[mid] > nums[right]) { left = mid + 1; } else { right = mid; } } return nums[left]; } int[] arr = {4,5,6,7,0,1,2}; Console.WriteLine(FindMin(arr));
Attempts:
2 left
💡 Hint
Think about how the binary search narrows down the smallest element in a rotated array.
✗ Incorrect
The code uses binary search to find the smallest element. Since the array is rotated, the smallest element is the pivot point where the order breaks. The code correctly returns 0, which is the minimum in the array.
❓ Predict Output
intermediateMinimum element in non-rotated sorted array
What will the following code output when the input array is sorted but not rotated?
DSA C#
int FindMin(int[] nums) { int left = 0, right = nums.Length - 1; while (left < right) { int mid = left + (right - left) / 2; if (nums[mid] > nums[right]) { left = mid + 1; } else { right = mid; } } return nums[left]; } int[] arr = {1,2,3,4,5,6,7}; Console.WriteLine(FindMin(arr));
Attempts:
2 left
💡 Hint
If the array is not rotated, the smallest element is the first element.
✗ Incorrect
Since the array is sorted and not rotated, the smallest element is the first element, which is 1. The binary search will return this correctly.
🔧 Debug
advancedIdentify the error in minimum finder code
What error will the following code produce when run with input {}?
DSA C#
int FindMin(int[] nums) { int left = 0, right = nums.Length - 1; while (left <= right) { int mid = left + (right - left) / 2; if (nums[mid] > nums[right]) { left = mid + 1; } else { right = mid - 1; } } return nums[left]; } int[] arr = {}; Console.WriteLine(FindMin(arr));
Attempts:
2 left
💡 Hint
Check the loop condition and how left and right pointers are updated.
✗ Incorrect
The loop condition uses <= and right is decreased by 1 in else, which can cause left to go beyond array length, causing IndexOutOfRangeException when accessing nums[left].
❓ Predict Output
advancedOutput of minimum finder with duplicates
What is the output of the following code when the input array contains duplicates?
DSA C#
int FindMin(int[] nums) { int left = 0, right = nums.Length - 1; while (left < right) { int mid = left + (right - left) / 2; if (nums[mid] > nums[right]) { left = mid + 1; } else if (nums[mid] < nums[right]) { right = mid; } else { right--; } } return nums[left]; } int[] arr = {2,2,2,0,1,2}; Console.WriteLine(FindMin(arr));
Attempts:
2 left
💡 Hint
Duplicates require careful handling to reduce the search space.
✗ Incorrect
The code handles duplicates by reducing right by one when nums[mid] equals nums[right]. The minimum element is 0, correctly found by the code.
🧠 Conceptual
expertWhy binary search works in rotated sorted array
Why does binary search still work to find the minimum element in a rotated sorted array?
Attempts:
2 left
💡 Hint
Think about how rotation affects the sorted order and how binary search uses sorted halves.
✗ 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, eventually finding the minimum element.
