The Find Peak Element interview question asks you to find any "peak" element in an array. A peak element is an element that is strictly greater than its neighbors. For elements at the boundaries, they only need to be greater than their single neighbor. You are guaranteed that nums[i] != nums[i+1] for all valid . The goal is to find the index of any peak in time.
This is a standard question at Apple, Amazon, and Google. It tests your ability to apply Binary Search to a non-sorted array. It evaluations if you understand the "climbing" property: if you are on a slope moving upwards, there must be a peak somewhere ahead of you (or at the end). It’s a core Binary Search interview pattern.
This follows the Binary Search on a Monotonic Slope pattern.
left = 0, right = n - 1.nums[mid] and nums[mid + 1].nums[mid] < nums[mid + 1], you are currently on an upward slope. A peak must exist to the right. Move left = mid + 1.nums[mid] > nums[mid + 1], you are on a downward slope or at a peak. A peak must exist to the left (including mid). Move right = mid.left == right, you've found a peak.Array: [1, 2, 3, 1]
left=0, right=3, mid=1 (2).left = 2.left=2, right=3, mid=2 (3).right = 2.left == right. Result index: 2 (value 3).mid + 1 trick avoids this).Binary Search doesn't always require a sorted array. It only requires a way to discard half of the search space. In this Array interview pattern, the "slope" provides that discard logic.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Find Minimum in Rotated Sorted Array | Medium | Solve | |
| Koko Eating Bananas | Medium | Solve | |
| Search in Rotated Sorted Array | Medium | Solve | |
| Find First and Last Position of Element in Sorted Array | Medium | Solve | |
| Single Element in a Sorted Array | Medium | Solve |