Bird
Raised Fist0
Interview Prepmodified-binary-searchhardAmazonGoogle

Find Minimum in Rotated Array II (With Duplicates)

Choose your preparation mode4 modes available
📋
Problem

Imagine you have a circularly shifted sorted list of timestamps from a sensor, but some readings are duplicated due to noise. You need to quickly find the earliest timestamp to synchronize data streams.

Given an integer array nums sorted in non-decreasing order, which is rotated at an unknown pivot, possibly containing duplicates, return the minimum element of this array. You must minimize the number of operations.

1 ≤ nums.length ≤ 10^5-10^4 ≤ nums[i] ≤ 10^4nums is rotated at some pivot unknown to you beforehandDuplicates are allowed
Edge cases: Array with all identical elements → minimum is that elementArray rotated at last element → minimum is first elementArray with two elements, rotated → minimum is smaller element
</>
IDE
def find_min(nums: list[int]) -> int:public int findMin(int[] nums)int findMin(vector<int> &nums)function findMin(nums)
def find_min(nums):
    # Write your solution here
    pass
class Solution {
    public int findMin(int[] nums) {
        // Write your solution here
        return 0;
    }
}
#include <vector>
using namespace std;

int findMin(vector<int> &nums) {
    // Write your solution here
    return 0;
}
function findMin(nums) {
    // Write your solution here
}
Coming soon
0/9
Common Bugs to Avoid
Wrong: 2Not shrinking high pointer when nums[mid] == nums[high], causing incorrect minimum selection.✅ Add condition to decrement high by one when nums[mid] == nums[high].
Wrong: 4Incorrectly choosing left half when nums[mid] > nums[high] without proper checks.✅ If nums[mid] > nums[high], search right half; else left half.
Wrong: 3Greedy approach picking half incorrectly when duplicates present, missing pivot.✅ When nums[mid] == nums[high], shrink high pointer by one instead of choosing half.
Wrong: IndexError or infinite loopMissing base case for single element or all identical elements causing infinite loop.✅ Add base case to return nums[0] if length is 1; shrink high pointer carefully for duplicates.
✓
Test Cases
t1_01basic
Input[2,2,2,0,1]
Expected0

The array was rotated and contains duplicates; the minimum element is 0.

t1_02basic
Input[4,5,6,7,0,1,2]
Expected0

Standard rotated array without duplicates; minimum element is 0 at pivot.

t2_01edge
Input[1]
Expected1

Single element array; minimum is the only element.

t2_02edge
Input[2,2,2,2,2]
Expected2

All elements identical; minimum is that element.

t2_03edge
Input[1,2]
Expected1

Two elements, not rotated; minimum is first element.

t3_01corner
Input[10,1,10,10,10]
Expected1

Duplicates with pivot in middle; tests correct shrinking of high pointer.

t3_02corner
Input[3,3,1,3]
Expected1

Tests confusion between 0/1 knapsack style and binary search; must not assume unique elements.

t3_03corner
Input[2,2,2,3,1,2]
Expected1

Greedy trap test: naive binary search picking wrong half due to duplicates.

t4_01performance
Input{"_description":"n=100000 at constraint boundary - executor generates this"}
⏱ Performance - must finish in 2000ms

Large input with n=100000 tests O(n) worst-case complexity due to duplicates; must complete within 2 seconds.

Practice

(1/5)
1. You need to ship packages within a given number of days. Each package has a weight, and you must ship packages in order without splitting any package. You want to find the minimum ship capacity to ship all packages within the deadline. Which algorithmic approach guarantees finding the optimal ship capacity efficiently?
easy
A. Greedy approach that assigns packages to days until capacity is reached, then moves to next day without binary search
B. Dynamic programming to find the minimum capacity by exploring all subsets of packages and days
C. Sorting packages and using a two-pointer technique to assign packages to days
D. Binary search on the answer space (capacity) combined with a feasibility check to verify if shipping within days is possible

Solution

  1. Step 1: Understand the problem constraints

    The problem requires finding a minimum capacity that allows shipping all packages in order within a fixed number of days.
  2. Step 2: Recognize the pattern

    Binary searching the capacity (answer space) and checking feasibility is the optimal approach because capacity ranges between max package weight and sum of all weights, and feasibility can be checked in linear time.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Binary search on answer space is classic for optimization with feasibility checks [OK]
Hint: Binary search on capacity range solves this optimally [OK]
Common Mistakes:
  • Thinking greedy alone finds minimal capacity
  • Confusing DP with subset sums
  • Using two pointers without sorting relevance
2. Given the following code and input nums = [2, 4, 4, 6] and target = 4, what is the final returned value?
easy
A. [1, 2]
B. [2, 3]
C. [0, 1]
D. [-1, -1]

Solution

  1. Step 1: Trace binary search to find any occurrence of 4

    Initial left=0, right=3, mid=1, nums[1]=4 matches target, found=1.
  2. Step 2: Expand to find leftmost and rightmost indices

    Start=1, check left: nums[0]=2 !=4 stop; end=1, check right: nums[2]=4 matches, end=2; nums[3]=6 stop.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Positions of 4 are indices 1 and 2 [OK]
Hint: Break binary search on first found, then expand boundaries [OK]
Common Mistakes:
  • Off-by-one errors expanding boundaries
  • Returning found index only
  • Confusing indices with values
3. Given the following code for finding a peak element, what is the final output when the input array is [1, 3, 2]?
def findPeakElement(nums):
    def binarySearch(low, high):
        if low == high:
            return low
        mid = (low + high) // 2
        if nums[mid] < nums[mid + 1]:
            return binarySearch(mid + 1, high)
        else:
            return binarySearch(low, mid)
    return binarySearch(0, len(nums) - 1)

print(findPeakElement([1, 3, 2]))
easy
A. None of the above
B. 0
C. 2
D. 1

Solution

  1. Step 1: Trace first call binarySearch(0, 2)

    mid = (0+2)//2 = 1; nums[1]=3, nums[2]=2; since 3 > 2, recurse binarySearch(0,1)
  2. Step 2: Trace second call binarySearch(0, 1)

    mid = (0+1)//2 = 0; nums[0]=1, nums[1]=3; since 1 < 3, recurse binarySearch(1,1)
  3. Step 3: Trace third call binarySearch(1, 1)

    low == high, return 1
  4. Final Answer:

    Option D -> Option D
  5. Quick Check:

    Returned index 1 corresponds to peak element 3 [OK]
Hint: Check mid and mid+1 comparison carefully [OK]
Common Mistakes:
  • Returning mid instead of low/high
  • Off-by-one in mid calculation
  • Confusing which half to recurse
4. The following code attempts to find the median of two sorted arrays using binary search on the smaller array. Identify the line containing the subtle bug that can cause incorrect median calculation for even total length arrays.
medium
A. Line: if (m + n) % 2 == 0: return (max(maxLeftX, maxLeftY) + min(minRightX, minRightY)) / 2
B. Line: if len(nums1) > len(nums2): nums1, nums2 = nums2, nums1
C. Line: partitionY = (m + n) // 2 - partitionX
D. Line: while low <= high:

Solution

  1. Step 1: Understand partition calculation

    partitionY must be total left half minus partitionX to split arrays correctly.
  2. Step 2: Identify off-by-one bug

    Using (m + n) // 2 instead of (m + n + 1) // 2 causes incorrect partition when total length is odd, leading to wrong median.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Partition calculation off-by-one breaks median correctness [OK]
Hint: Partition indices must sum to half total length exactly, using (m+n+1)//2 for correct odd/even handling [OK]
Common Mistakes:
  • Swapping arrays incorrectly
  • Wrong median formula for even length
  • Loop boundary off-by-one
5. Suppose the matrix rows are sorted but columns are not guaranteed to be sorted. Which of the following approaches correctly finds the target value efficiently?
hard
A. Use the staircase search starting from the top-right corner.
B. Use a brute force nested loop to check every element.
C. Perform binary search on each row independently.
D. Sort each column first, then apply staircase search.

Solution

  1. Step 1: Understand matrix constraints

    Only rows are sorted; columns are unsorted, so staircase search assumptions fail.
  2. Step 2: Evaluate approaches

    Binary search on rows works but columns unsorted means target could be anywhere in columns; brute force guarantees correctness.
  3. Step 3: Consider sorting columns

    Sorting columns is expensive and changes matrix; not practical for search.
  4. Final Answer:

    Option B -> Option B
  5. Quick Check:

    Brute force is correct though less efficient; others rely on column sorting [OK]
Hint: Without column sorting, staircase search fails [OK]
Common Mistakes:
  • Assuming staircase search works with only row sorting
  • Thinking sorting columns is cheap
  • Believing binary search on rows suffices
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard