Challenge - 5 Problems
Rotated Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateFind the minimum element output in a rotated sorted array
What is the output of the following code that finds the minimum element in a rotated sorted array?
DSA Python
def find_min(nums): left, right = 0, len(nums) - 1 while left < right: mid = (left + right) // 2 if nums[mid] > nums[right]: left = mid + 1 else: right = mid return nums[left] print(find_min([4,5,6,7,0,1,2]))
Attempts:
2 left
💡 Hint
Think about how the binary search narrows down the smallest element by comparing middle and right values.
✗ Incorrect
The code uses binary search to find the smallest element in a rotated sorted array. It compares the middle element with the rightmost element to decide which half to search next. The minimum is 0 in the given array.
❓ Predict Output
intermediateOutput of minimum finder on a non-rotated sorted array
What will the output be when the minimum finder function is run on a sorted array that is not rotated?
DSA Python
def find_min(nums): left, right = 0, len(nums) - 1 while left < right: mid = (left + right) // 2 if nums[mid] > nums[right]: left = mid + 1 else: right = mid return nums[left] print(find_min([1,2,3,4,5,6,7]))
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.
🔧 Debug
advancedIdentify the error in this minimum finder code
What error will this code produce when trying to find the minimum in a rotated sorted array?
DSA Python
def find_min(nums): left, right = 0, len(nums) - 1 while left <= right: mid = (left + right) // 2 if nums[mid] > nums[right]: left = mid + 1 else: right = mid - 1 return nums[left] print(find_min([3,4,5,1,2]))
Attempts:
2 left
💡 Hint
Check the loop condition and how left and right pointers move.
✗ Incorrect
The loop condition 'left <= right' combined with 'right = mid - 1' can cause left to go out of range, leading to IndexError when accessing nums[left].
❓ Predict Output
advancedOutput of minimum finder on array with duplicates
What is the output of the following code when the rotated sorted array contains duplicates?
DSA Python
def find_min(nums): left, right = 0, len(nums) - 1 while left < right: mid = (left + right) // 2 if nums[mid] > nums[right]: left = mid + 1 elif nums[mid] < nums[right]: right = mid else: right -= 1 return nums[left] print(find_min([2,2,2,0,1,2]))
Attempts:
2 left
💡 Hint
Duplicates require extra handling by reducing right pointer when equal.
✗ Incorrect
The code handles duplicates by decreasing right when nums[mid] equals nums[right], ensuring the minimum 0 is found.
🧠 Conceptual
expertWhy binary search works for finding minimum in rotated sorted array
Why does binary search correctly find the minimum element in a rotated sorted array?
Attempts:
2 left
💡 Hint
Think about how rotation affects sorting and how comparisons guide the search.
✗ Incorrect
Binary search works because the rotated array is made of two sorted parts. Comparing middle and right elements helps decide which part contains the minimum, allowing discarding half the array each step.
