Binary Search is simple. But Amazon rarely asks vanilla binary search.
Instead, they twist it with rotations, conditions, peaks, or boundaries.
π This pattern helps solve:
- Rotated sorted arrays
- Find element in infinite arrays
- Peak / Mountain array problems
- First/Last occurrence of element
π Problem 1: Search in Rotated Sorted Array
π Amazon-style phrasing:
Given a rotated sorted array, return the index of target if found, else -1.
Java Solution
public class SearchRotatedArray {
public static int search(int[] nums, int target) {
int left = 0, right = nums.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] == target) return mid;
if (nums[left] <= nums[mid]) { // left half sorted
if (target >= nums[left] && target < nums[mid]) {
right = mid - 1;
} else {
left = mid + 1;
}
} else { // right half sorted
if (target > nums[mid] && target <= nums[right]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
}
return -1;
}
}
β
Time Complexity: O(log n)
β
Amazon Insight: Always test if the half is sorted to decide where to go.
π Problem 2: Find Minimum in Rotated Sorted Array
π Amazon-style phrasing:
Given a rotated sorted array, find the minimum element.
Java Solution
public class FindMinRotated {
public static int findMin(int[] nums) {
int left = 0, right = nums.length - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] > nums[right]) {
left = mid + 1; // min on right side
} else {
right = mid; // min on left including mid
}
}
return nums[left];
}
}
β
Amazon Insight:
Tests your ability to adapt binary search without a target.
π Problem 3: Peak Element (Find Local Maximum)
π Amazon-style phrasing:
Find a peak element in the array. A peak is greater than its neighbors.
Java Solution
public class PeakElement {
public static int findPeakElement(int[] nums) {
int left = 0, right = nums.length - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] < nums[mid + 1]) {
left = mid + 1; // go right
} else {
right = mid; // go left (mid might be peak)
}
}
return left;
}
}
β
Amazon Insight:
Binary Search applied even when array isnβt sorted.
π Problem 4: Find Element in Infinite Sorted Array
π Amazon-style phrasing:
You have an infinite sorted array. Find the position of target.
β‘ Trick:
- Expand range exponentially (
1, 2, 4, 8...) - Then apply binary search.
Java Solution
public class InfiniteArraySearch {
public static int searchInfiniteArray(int[] nums, int target) {
int left = 0, right = 1;
while (right < nums.length && nums[right] < target) {
left = right;
right *= 2; // expand window
if (right >= nums.length) right = nums.length - 1;
}
return binarySearch(nums, left, right, target);
}
private static int binarySearch(int[] nums, int left, int right, int target) {
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] == target) return mid;
if (nums[mid] < target) left = mid + 1;
else right = mid - 1;
}
return -1;
}
}
β
Amazon Insight:
Tests scalability thinking β how to search without knowing the array size.
π Problem 5: First and Last Position of Element
π Amazon-style phrasing:
Find the first and last position of target in sorted array.
Java Solution
public class FirstLastPosition {
public static int[] searchRange(int[] nums, int target) {
return new int[]{findBound(nums, target, true), findBound(nums, target, false)};
}
private static int findBound(int[] nums, int target, boolean isFirst) {
int left = 0, right = nums.length - 1, result = -1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] == target) {
result = mid;
if (isFirst) right = mid - 1;
else left = mid + 1;
} else if (nums[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return result;
}
}
β
Amazon Insight:
Tests how well you adapt binary search to find boundaries.
π Extended Problem List (Amazon Variations)
- Search in Rotated Sorted Array II (with duplicates)
- Find Peak in Mountain Array (LeetCode 1095 β Amazon loves this)
- Square Root using Binary Search
- Allocate Minimum Pages / Split Array Largest Sum (binary search on answer)
- Aggressive Cows / Capacity to Ship Packages
π― Key Takeaways
- Modified Binary Search = framework adaptation.
- Always ask: What condition tells me which side to search?
- Variants: rotation, boundaries, infinite, peaks, binary search on answers.
π
Next in the series (Day 12):
π Top-K Elements Pattern β heaps + priority queues, super important for Amazon.
Top comments (0)