Magicsheet logo

Find Minimum in Rotated Sorted Array II

Hard
12.5%
Updated 6/1/2025

Asked by 3 Companies

Find Minimum in Rotated Sorted Array II

What is this problem about?

The Find Minimum in Rotated Sorted Array II interview question is the more difficult version of the rotation problem because it allows duplicate elements. In an array like [2, 2, 2, 0, 1, 2], the presence of duplicates makes it impossible to know for sure which half contains the minimum using a single comparison. This Find Minimum in Rotated Sorted Array II coding problem requires a slightly more cautious search strategy.

Why is this asked in interviews?

Google and Microsoft ask this to see how you handle "worst-case" scenarios in algorithms. In the presence of duplicates, the time complexity of binary search can degrade to O(N)O(N). This question tests your ability to identify these edge cases and implement a robust fallback mechanism while still maintaining O(logN)O(\log N) average performance.

Algorithmic pattern used

This follows the Binary Search pattern with a Linear Fallback.

  1. Compare nums[mid] with nums[right].
  2. If nums[mid] > nums[right], search the right half.
  3. If nums[mid] < nums[right], search the left half (including mid).
  4. The Twist: If nums[mid] == nums[right], we don't know which way to go. We can only safely discard the last element by doing right--. This ensures we don't lose the minimum while slowly shrinking the window.

Example explanation

Array: [10, 1, 10, 10, 10]

  • left=0 (10), right=4 (10), mid=2 (10).
  • 10==1010 == 10, so we just do right = 3. Array is [10, 1, 10, 10].
  • left=0 (10), right=3 (10), mid=1 (1).
  • 1<101 < 10, so move right = 1. Array is [10, 1].
  • left=0 (10), right=1 (1), mid=0 (10).
  • 10>110 > 1, so move left = 1.
  • left == right. Result: 1.

Common mistakes candidates make

  • Assuming O(logN)O(\log N) always: Not explaining that the complexity can be O(N)O(N) if all elements are the same (e.g., [1, 1, 1, 1, 1]).
  • Ignoring the Duplicate Condition: Using the logic from the first version, which fails on arrays where the boundaries are identical.
  • Incorrect Indexing: Comparing mid with left instead of right, which makes handling the duplicates more complex.

Interview preparation tip

Whenever duplicates are introduced to a sorted search problem, expect the complexity to change. Practice the "shrink the boundary" technique (right--) as it is a common way to handle ambiguity in Binary Search interview patterns.

Similar Questions