MediumArrayBinary SearchUpdated Sep 2026

162. Find Peak Element

Asked at Uber, Bloomberg

Difficulty

Medium

Time

O(log n)

Space

O(1)

Pattern

Array

Companies

2

Problem

You are given an integer array nums. An element is called a peak if it is strictly larger than the element on its left and strictly larger than the element on its right.

Treat the positions just outside the array as if they held negative infinity. So the first element only needs to beat its right neighbor to be a peak, and the last element only needs to beat its left neighbor. This also means a peak always exists.

Return the index of any peak element. If several peaks exist, any one of them is accepted. Your algorithm must run in O(log n) time, so a plain left-to-right scan is not good enough.

A peak beats both sides

nums123101233 is greater than 2 on the left and 1 on the right
Index 2 holds 3, which is larger than both of its neighbors, so it is a peak.

Constraints

  • ●1 <= nums.length <= 1000
  • ●-2^31 <= nums[i] <= 2^31 - 1
  • ●nums[i] != nums[i + 1] for all valid i

Examples

Example 1
Inputnums = [1,2,3,1]
Output2
Whynums[2] = 3 is bigger than nums[1] = 2 and nums[3] = 1, so index 2 is a peak.
Example 2
Inputnums = [1,2,1,3,5,6,4]
Output5
Whynums[5] = 6 beats nums[4] = 5 and nums[6] = 4. Index 1 (value 2) is also a peak, so returning 1 would be accepted too.

Asked At

2 companies
CompanyDifficulty
UberMediumView all Uber questions →
BloombergMediumView all Bloomberg questions →

How to Think About It

1

Brute force: Linear scan every element, check if it is greater than both neighbors.

2

Optimized scan: Move in the direction of the larger neighbor to avoid checking every element.

3

Binary search idea: If mid is less than its right neighbor, the peak lies on the right side.

4

If mid is less than its left neighbor, the peak lies on the left side; otherwise mid itself is a peak.

5

Binary search achieves O(log n) by halving the search space each iteration.

Optimal Approach

Use binary search on the array. Compare nums[mid] with nums[mid + 1]. If nums[mid] < nums[mid + 1], the peak must be to the right, so set left = mid + 1. Otherwise, the peak is to the left or at mid itself, so set right = mid. When left equals right, that index is guaranteed to be a peak because at least one peak always exists in such an array.

Binary search on the slope

nums12310123leftrightStart with the whole array: left = 0, right = 3nums12310123leftrightmid = 1: nums[1] = 2 < nums[2] = 3, the slope goes up,so the peak is to the rightnums12310123left = rightleft becomes 2. Now left = right, so index 2 is theanswerif nums[mid] < nums[mid+1]: left = mid + 1else: right = midstop when left == right
We compare the middle with its right neighbor and keep only the half that must contain a peak.
ComplexityTime O(log n)Space O(1)Each step throws away half of the remaining range, and only a couple of variables are kept.

What Trips People Up in Real Interviews

1

Clarify whether equal adjacent values are allowed.

2

Mention that there can be multiple valid answers; returning any one suffices.

3

Explain why binary search works by arguing monotonic direction toward a peak.

4

Edge cases: single element array, peak at index 0 or last index.

5

State the time complexity as O(log n) and space as O(1).

Solution Code

O(log n) time
def findPeakElement(nums):
    left, right = 0, len(nums) - 1
    while left < right:
        mid = (left + right) // 2
        if nums[mid] < nums[mid + 1]:
            left = mid + 1
        else:
            right = mid
    return left

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

Frequently Asked Questions

What is the Find Peak Element problem?

A peak element is strictly greater than its neighbors. Given an input array nums where nums[i] != nums[i+1], find a peak element and return its index. You must solve it in O(log n) time.

How do you solve Find Peak Element?

The optimal approach runs in O(log n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.

What companies ask Find Peak Element?

Find Peak Element is asked at Uber, Bloomberg. It is a medium difficulty problem.

What are common mistakes on Find Peak Element?
  • Clarify whether equal adjacent values are allowed.
  • Mention that there can be multiple valid answers; returning any one suffices.
  • Explain why binary search works by arguing monotonic direction toward a peak.
  • Edge cases: single element array, peak at index 0 or last index.
  • State the time complexity as O(log n) and space as O(1).