Bird
Raised Fist0
Interview Prepmodified-binary-searchmediumAmazonGoogle

Search in Rotated Array II (Duplicates)

Choose your preparation mode4 modes available
</>
IDE
def search(nums: list[int], target: int) -> bool:public boolean search(int[] nums, int target)bool search(vector<int>& nums, int target)function search(nums, target)
def search(nums, target):
    # Write your solution here
    pass
class Solution {
    public boolean search(int[] nums, int target) {
        // Write your solution here
        return false;
    }
}
#include <vector>
using namespace std;

bool search(vector<int>& nums, int target) {
    // Write your solution here
    return false;
}
function search(nums, target) {
    // Write your solution here
}
Coming soon
0/10
Common Bugs to Avoid
Wrong: falseFails to find target when duplicates cause ambiguity and search space is not shrunk properly.✅ Add logic to shrink both ends when nums[left] == nums[mid] == nums[right].
Wrong: trueReturns true incorrectly when target is not present due to incorrect mid comparisons.✅ Ensure comparisons correctly exclude impossible halves and only return true when nums[mid] == target.
Wrong: runtime error or crashDoes not handle empty array or single element arrays properly causing index errors.✅ Add explicit checks for empty and single-element arrays before binary search loop.
Wrong: timeoutAlgorithm degenerates to O(n^2) due to not shrinking search space when duplicates equal mid.✅ Implement shrinking both ends when nums[left] == nums[mid] == nums[right] to reduce search space.
✓
Test Cases
t1_01basic
Input{"nums":[2,5,6,0,0,1,2],"target":0}
Expectedtrue

Target 0 is present in the rotated array.

t1_02basic
Input{"nums":[4,5,6,7,0,1,2],"target":3}
Expectedfalse

Target 3 is not present in the rotated array.

t2_01edge
Input{"nums":[],"target":1}
Expectedfalse

Empty array cannot contain any target.

t2_02edge
Input{"nums":[1],"target":1}
Expectedtrue

Single element array where target is present.

t2_03edge
Input{"nums":[1],"target":0}
Expectedfalse

Single element array where target is absent.

t2_04edge
Input{"nums":[1,1,1,1],"target":1}
Expectedtrue

Array with all duplicates where target is present.

t3_01corner
Input{"nums":[1,1,3,1],"target":3}
Expectedtrue

Duplicates cause ambiguity; target 3 is present.

t3_02corner
Input{"nums":[2,2,2,3,2,2,2],"target":3}
Expectedtrue

Target in middle with duplicates around; tests correct mid handling.

t3_03corner
Input{"nums":[1,3,1,1,1],"target":3}
Expectedtrue

Rotated array with duplicates and target in rotated segment.

t4_01performance
Input{"nums":[10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000],"target":1}
⏱ Performance - must finish in 2000ms

n=300, worst case O(n) due to duplicates, must complete within 2s.

Practice

(1/5)
1. Given the following code snippet, what is the final return value when calling minDays([1,10,3,10,2], 3, 1)?
easy
A. 2
B. 1
C. 3
D. -1

Solution

  1. Step 1: Trace binary search boundaries

    min bloom day = 1, max bloom day = 10. Binary search mid values: 5, then 3, then 2.
  2. Step 2: Check feasibility at mid=5 and mid=2

    At day=5, canMake returns true (flowers bloomed on days 1,3,2). Try lower days. At day=2, canMake returns true as well. At day=1, canMake returns false. So minimum day is 2.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Tracing confirms minimum feasible day is 2. [OK]
Hint: Check feasibility at midpoints to narrow minimum day [OK]
Common Mistakes:
  • Off-by-one errors in binary search boundaries
  • Miscounting bouquets in feasibility check
2. You need to find the minimum speed to travel through a series of distances within a given total time, where each segment except the last must be completed in whole hours (rounded up). Which algorithmic approach guarantees finding the minimum feasible speed efficiently?
easy
A. Binary search on the speed range (answer space) combined with a feasibility check
B. Brute force linear search incrementing speed until the time constraint is met
C. Dynamic programming to store minimum speeds for subproblems
D. Greedy approach by always choosing the fastest possible speed for each segment

Solution

  1. Step 1: Understand the problem constraints

    The problem requires finding the minimum speed that satisfies a time constraint with rounding up travel times for all but the last segment.
  2. Step 2: Identify the suitable algorithmic pattern

    Binary search on the answer space (speed) efficiently narrows down the minimum feasible speed by checking feasibility at midpoints, unlike greedy or DP which do not fit the problem structure.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Binary search on answer space is standard for optimization problems with monotonic feasibility [OK]
Hint: Binary search on answer space fits monotonic feasibility [OK]
Common Mistakes:
  • Thinking greedy or DP can solve this efficiently
  • Using brute force linear search without pruning
3. Consider the following code snippet that searches for a target in a 2D matrix using binary search treating it as a 1D array. Given the matrix = [[1,4],[6,8]] and target = 6, what is the value of mid and the corresponding row, col during the first iteration of the while loop?
easy
A. mid = 1, row = 0, col = 1
B. mid = 2, row = 1, col = 0
C. mid = 0, row = 0, col = 0
D. mid = 3, row = 1, col = 1

Solution

  1. Step 1: Calculate initial boundaries and mid

    m=2, n=2, left=0, right=3, so mid=(0+3)//2=1.
  2. Step 2: Map mid to 2D indices

    row, col = divmod(1, 2) -> row=0, col=1.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    mid=1 corresponds to matrix[0][1]=4, correct mapping [OK]
Hint: mid index maps to row,col via divmod(mid, n) [OK]
Common Mistakes:
  • Off-by-one in mid calculation
  • Confusing row and col indices
  • Using mid as direct index without divmod
4. Examine the following buggy code that attempts to search a target in a 2D matrix using the staircase search approach starting from the top-left corner. Identify the line containing the subtle bug that causes incorrect behavior or infinite loops.
medium
A. Line 9: Incrementing row when element is less than target
B. Line 7: Using '<' instead of '<=' in while condition
C. Line 11: Decrementing col without boundary check
D. Line 3: Starting at top-left corner instead of top-right

Solution

  1. Step 1: Analyze starting point

    Starting at top-left corner breaks the staircase search logic because we cannot eliminate rows or columns correctly.
  2. Step 2: Check pointer movements

    Moving row down when current < target and col left when current > target only works if starting at top-right corner.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Starting at top-left causes infinite loops or wrong results [OK]
Hint: Staircase search must start at top-right corner [OK]
Common Mistakes:
  • Starting at top-left or bottom-left corner
  • Not checking empty matrix
  • Moving pointers in wrong directions
5. Suppose the array is no longer sorted but you still need to find the first and last positions of a target value. Which approach is correct and efficient?
hard
A. Use a linear scan to find first and last occurrences directly
B. Sort the array first, then apply modified binary search to find positions
C. Use the same modified binary search approach since it works on any array
D. Use a hash map to store all indices of each value, then retrieve target positions

Solution

  1. Step 1: Recognize that binary search requires sorted array

    Without sorting, binary search cannot guarantee correct positions.
  2. Step 2: Evaluate alternatives

    Sorting changes indices, so linear scan or hash map are options; linear scan is simplest and O(n).
  3. Step 3: Compare efficiency and correctness

    Linear scan finds first and last positions in one pass without modifying array.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Binary search requires sorted input; linear scan works otherwise [OK]
Hint: Binary search needs sorted array; linear scan works otherwise [OK]
Common Mistakes:
  • Trying binary search on unsorted array
  • Sorting and losing original indices
  • Overcomplicating with hash maps
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