Skip to main content

83. Find Peak Element

mediumAsked at Vercel

Short answer

  • Best approach: Binary search on slope. If nums[mid] < nums[mid+1], a peak must exist in [mid+1, hi]. Else in [lo, mid]. Narrow until lo == hi.
  • Time O(log n), space O(1)
  • How often Vercel asks it: sometimes asked · Verified

Find any peak in an array (element greater than its neighbors) in O(log n). Vercel asks this for the 'binary search on a non-sorted array' insight — same trick they use to find the highest-traffic edge node in a request-rate gradient.

By Sam K., Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Vercel loops.

  • Glassdoor (2025-Q4)— Vercel platform onsite; O(log n) binary search expected.
  • Blind (2026-Q1)— Listed in Vercel screen pool.

Problem

A peak element is an element that is strictly greater than its neighbors. Given a 0-indexed integer array nums, find a peak element, and return its index. If the array contains multiple peaks, return the index to any of the peaks. You may imagine that nums[-1] = nums[n] = -Infinity. You must write an algorithm that runs in O(log n) time.

Constraints

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

Examples

Example 1

Input
nums = [1,2,3,1]
Output
2

Explanation: 3 is a peak element.

Example 2

Input
nums = [1,2,1,3,5,6,4]
Output
5

Explanation: Index 5 (value 6) is a peak.

Approaches

1. Linear scan

Walk and check if each element is a peak.

Time
O(n)
Space
O(1)
// O(n). Vercel wants O(log n).

Tradeoff: Easy but doesn't meet the constraint.

2. Binary search on slope (optimal)

If nums[mid] < nums[mid+1], a peak must exist in [mid+1, hi]. Else in [lo, mid]. Narrow until lo == hi.

Time
O(log n)
Space
O(1)
function findPeakElement(nums) {
  let lo = 0, hi = nums.length - 1;
  while (lo < hi) {
    const mid = (lo + hi) >>> 1;
    if (nums[mid] < nums[mid + 1]) lo = mid + 1;
    else hi = mid;
  }
  return lo;
}

Tradeoff: O(log n) on an unsorted array — possible because of the 'peak must exist' guarantee from the implicit -infinity boundaries.

Vercel-specific tips

Vercel grades the slope-binary-search insight. Bonus signal: explaining WHY this works (going uphill leads to a peak before falling off the end) and noting that the implicit -infinity at both ends guarantees a peak exists.

Common mistakes

  • Comparing mid with both neighbors — over-checks; just one comparison suffices.
  • Using lo <= hi — runs past the answer.
  • Off-by-one on mid + 1 — must check bound before accessing.

Follow-up questions

An interviewer at Vercel may pivot to one of these next:

  • Find ALL peaks — linear.
  • Find the GLOBAL maximum — different problem (requires O(n)).
  • Find Peak in a 2D matrix (LC 1901).

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Ctrl+Enter to execute

FAQ

Why does binary search work without sorted input?

Because we use the SLOPE, not the value. If nums[mid] < nums[mid+1], we're going up; a peak must lie to the right before we fall off the (implicit -infinity) end. This is enough invariant for binary search.

Why is the answer guaranteed to exist?

Imagine -infinity at both ends. Any non-empty array has a global max which is strictly greater than both implicit endpoints, so it's a peak.

Originally published on InterviewChamp.AI

Companies that also ask Find Peak Element