By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
(Complete Guide for FAANG-Level Interviews)
"This problem appears in 1 out of every 3 onsite interviews—master it, and you prove you can handle binary search on non-standard inputs, a skill that separates L4 from L5 engineers."
Before tackling this problem, ensure you understand: 1. Binary Search – How to implement it, its time complexity (O(log n)), and edge cases (empty array, single element). 2. Rotated Sorted Arrays – How a sorted array looks after rotation (e.g., [4,5,6,7,0,1,2] is [0,1,2,4,5,6,7] rotated at index 3). 3. Invariants in Binary Search – How to maintain the "search space" and adjust pointers based on conditions.
O(log n)
[4,5,6,7,0,1,2]
[0,1,2,4,5,6,7]
nums[mid]
nums[right]
nums[mid] > nums[right]
[3,1,3]
nums[mid] == nums[right]
right
(Repeatable mental checklist for every problem of this type.)
Goal: Find the minimum element (or pivot index).
Initialize Pointers
left = 0, right = len(nums) - 1.
left = 0
right = len(nums) - 1
Binary Search Loop
While left < right:
left < right
mid = left + (right - left) // 2
left = mid + 1
nums[mid] < nums[right]
right = mid
right -= 1
Termination
When left == right, return nums[left] (the minimum).
left == right
nums[left]
Edge Cases
None
Already sorted array → Return nums[0].
nums[0]
Time/Space Complexity
O(n)
O(1)
Problem: Find the minimum in [4,5,6,7,0,1,2].
Step-by-Step: 1. left = 0, right = 6. 2. Iteration 1: - mid = 3 (nums[3] = 7). - 7 > nums[6] (2) → Pivot is in the right half (left = 4). 3. Iteration 2: - mid = 5 (nums[5] = 1). - 1 < nums[6] (2) → Pivot is in the left half (right = 5). 4. Iteration 3: - mid = 4 (nums[4] = 0). - 0 < nums[5] (1) → Pivot is in the left half (right = 4). 5. Termination: left == right → Return nums[4] = 0.
right = 6
mid = 3
nums[3] = 7
7 > nums[6] (2)
left = 4
mid = 5
nums[5] = 1
1 < nums[6] (2)
right = 5
mid = 4
nums[4] = 0
0 < nums[5] (1)
right = 4
Code (Python):
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]
Complexity: O(log n) time, O(1) space.
Why this works: - The key insight is that in a rotated sorted array, one half is always sorted. By comparing nums[mid] with nums[right], we determine which half contains the pivot.
Problem: Find the minimum in [3,1,3].
Step-by-Step: 1. left = 0, right = 2. 2. Iteration 1: - mid = 1 (nums[1] = 1). - 1 < nums[2] (3) → Pivot is in the left half (right = 1). 3. Termination: left == right → Return nums[1] = 1.
right = 2
mid = 1
nums[1] = 1
1 < nums[2] (3)
right = 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 elif nums[mid] < nums[right]: right = mid else: # nums[mid] == nums[right] right -= 1 return nums[left]
Complexity: O(log n) average, O(n) worst case (e.g., [1,1,1,1]).
[1,1,1,1]
Why this works: - When nums[mid] == nums[right], we can’t determine which half is sorted, so we decrement right to skip duplicates.
Problem: Return the index of the minimum element in [4,5,6,7,0,1,2].
Step-by-Step: 1. Same as Example 1, but return left instead of nums[left].
left
def findMinIndex(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 left
Why this works: - The pivot index is where the rotation occurs, and binary search efficiently narrows it down.
right = mid - 1
[1,1,1]
if not nums: return None
"Alright, let’s lock this in. Here’s the 30-second version for your whiteboard: 1. Binary search is your tool—initialize left and right. 2. Compare nums[mid] with nums[right]: - If nums[mid] > nums[right], the pivot is in the right half (left = mid + 1). - If nums[mid] < nums[right], the pivot is in the left half (right = mid). - If they’re equal, skip duplicates (right -= 1). 3. Terminate when left == right—that’s your minimum. 4. Edge cases: Empty array, single element, already sorted. 5. Time complexity: O(log n) best case, O(n) with duplicates.
Now go crush that interview. You’ve got this!
[2,2,2,0,1]
[1,3,5]
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.