By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
"This problem appears in 1 out of every 3 onsite interviews—master it, and you prove you can handle non-linear search spaces, a skill that separates L4 from L5 engineers at FAANG."
Before tackling this problem, ensure you understand: 1. Binary Search – Must know how to implement it from scratch (recursive/iterative). 2. Sorted Arrays & Rotations – Understand how a sorted array behaves when rotated (e.g., [4,5,6,1,2,3]). 3. Edge Cases in Arrays – Empty arrays, single-element arrays, duplicates.
[4,5,6,1,2,3]
nums[mid]
nums[left]
nums[right]
[1,1,1,2,1]
nums[mid] == target
(Repeatable mental checklist for every "Rotated Sorted Array" problem.)
-1
If the array has 1 element → check if it matches the target.
Initialize Binary Search Pointers
left = 0, right = len(nums) - 1.
left = 0
right = len(nums) - 1
Binary Search Loop (while left <= right)
while left <= right
mid = left + (right - left) // 2
If nums[mid] == target → return mid.
mid
Determine Which Half is Sorted
nums[left] <= nums[mid]
nums[left] <= target < nums[mid]
right = mid - 1
left = mid + 1
Else → right half is sorted.
nums[mid] < target <= nums[right]
Handle Duplicates (if applicable)
If nums[left] == nums[mid] == nums[right] → increment left and decrement right to skip duplicates.
nums[left] == nums[mid] == nums[right]
left
right
Return -1 if Not Found
Problem: Search for target = 6 in [4,5,6,1,2,3].
target = 6
Step-by-Step: 1. Edge Case: Array is not empty → proceed. 2. Initialize: left = 0, right = 5. 3. First Iteration (mid = 2): - nums[2] = 6 == target → return 2.
right = 5
mid = 2
nums[2] = 6 == target
2
Code (Python):
def search(nums, target): left, right = 0, len(nums) - 1 while left <= right: mid = left + (right - left) // 2 if nums[mid] == target: return mid # Left half is sorted if nums[left] <= nums[mid]: if nums[left] <= target < nums[mid]: right = mid - 1 else: left = mid + 1 # Right half is sorted else: if nums[mid] < target <= nums[right]: left = mid + 1 else: right = mid - 1 return -1
Time Complexity: O(log n) (standard binary search). Space Complexity: O(1) (iterative approach).
O(log n)
O(1)
Why This Works: - The array is partially sorted, so we can still apply binary search by checking which half is sorted and whether the target lies in that range.
Problem: Search for target = 2 in [1,1,1,2,1,1].
target = 2
[1,1,1,2,1,1]
Step-by-Step: 1. Edge Case: Array is not empty → proceed. 2. Initialize: left = 0, right = 5. 3. First Iteration (mid = 2): - nums[2] = 1 != target. - nums[left] == nums[mid] == nums[right] == 1 → skip duplicates (left++, right--). 4. Second Iteration (left = 1, right = 4, mid = 2): - nums[2] = 1 != target. - nums[left] = 1 <= nums[mid] = 1 → left half is sorted. - 1 <= 2 < 1 → False → search right (left = mid + 1). 5. Third Iteration (left = 3, right = 4, mid = 3): - nums[3] = 2 == target → return 3.
nums[2] = 1 != target
nums[left] == nums[mid] == nums[right] == 1
left++
right--
left = 1
right = 4
nums[left] = 1 <= nums[mid] = 1
1 <= 2 < 1
left = 3
mid = 3
nums[3] = 2 == target
3
def search(nums, target): left, right = 0, len(nums) - 1 while left <= right: mid = left + (right - left) // 2 if nums[mid] == target: return True # Skip duplicates if nums[left] == nums[mid] == nums[right]: left += 1 right -= 1 # Left half is sorted elif nums[left] <= nums[mid]: if nums[left] <= target < nums[mid]: right = mid - 1 else: left = mid + 1 # Right half is sorted else: if nums[mid] < target <= nums[right]: left = mid + 1 else: right = mid - 1 return False
Time Complexity: O(n) in worst case (all duplicates), O(log n) average case. Space Complexity: O(1).
O(n)
Why This Works: - Duplicates break binary search’s invariants, so we skip them while maintaining the search space.
Problem: Find the smallest element in [4,5,6,1,2,3].
Step-by-Step: 1. Edge Case: If array is not rotated (nums[left] < nums[right]) → return nums[left]. 2. Initialize: left = 0, right = 5. 3. Binary Search: - mid = 2 → nums[mid] = 6 > nums[right] = 3 → search right (left = mid + 1). - mid = 4 → nums[mid] = 2 < nums[right] = 3 → search left (right = mid). - left == right → return nums[left] = 1.
nums[left] < nums[right]
nums[mid] = 6 > nums[right] = 3
mid = 4
nums[mid] = 2 < nums[right] = 3
right = mid
left == right
nums[left] = 1
def findMin(nums): left, right = 0, len(nums) - 1 while left < right: mid = left + (right - left) // 2 if nums[mid] > nums[right]: left = mid + 1 else: right = mid return nums[left]
Time Complexity: O(log n). Space Complexity: O(1).
Why This Works: - The smallest element is the only point where nums[i] > nums[i+1] in a rotated sorted array.
nums[i] > nums[i+1]
mid = (left + right) // 2
nums[mid] < nums[right]
while left < right
left <= right
[1,1,1,1]
"Alright, let’s lock this in. When you see a rotated sorted array, remember:
You’ve got this. Now go crush that interview!
Now go solve it like a FAANG engineer! ?
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.