Skip to main content

69. Find Peak Element

mediumAsked at Salesforce

Find any peak (local maximum) in an array in O(log n). Salesforce uses this to test binary search on a non-monotonic predicate.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Salesforce loops.

  • Glassdoor (2026-Q1)Salesforce uses this to gauge advanced binary-search comfort.
  • Blind (2025)Common Salesforce backend interview question.

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 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; index 2.

Example 2

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

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

Approaches

1. Linear scan

Walk left to right; return first index where nums[i] > nums[i+1].

Time
O(n)
Space
O(1)
function findPeakElement(nums) {
  for (let i = 0; i < nums.length - 1; i++) {
    if (nums[i] > nums[i + 1]) return i;
  }
  return nums.length - 1;
}

Tradeoff: Violates the explicit O(log n) requirement.

2. Binary search on slope

If nums[mid] > nums[mid+1], peak is in [lo, mid]. Else peak is in [mid+1, 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]) hi = mid;
    else lo = mid + 1;
  }
  return lo;
}

Tradeoff: O(log n). The key insight: at every step, follow the upward slope — a peak must exist in that direction.

Salesforce-specific tips

Salesforce uses this to test 'binary search beyond sorted' — the algorithm works because the boundary conditions (nums[-1] = nums[n] = -infinity) guarantee a peak exists. Bonus signal: articulate that the algorithm doesn't require a sorted input; it just follows the upward direction.

Common mistakes

  • Using lo <= hi — infinite loop when lo == hi at the answer.
  • Comparing nums[mid] > nums[mid-1] — needs the right boundary check and inverts the logic.
  • Returning -1 when no peak — by the problem's structure, a peak always exists.

Follow-up questions

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

  • Find Peak Element in 2D matrix.
  • Find Minimum in Rotated Sorted Array (LC 153) — similar binary search.
  • Why does the algorithm always terminate at a peak?

Solve it now

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

Output

Press Run or Cmd+Enter to execute

FAQ

Why does this always find a peak?

We assume nums[-1] = nums[n] = -infinity. At every step we narrow to the half with the upward slope; that half must contain a peak (it can't all be flat or downward because the imaginary boundary is lower).

What if all elements are equal?

The constraint forbids it (nums[i] != nums[i+1]). Without that constraint, any binary-search approach is ambiguous.

Practice these live with InterviewChamp.AI

Drill Find Peak Element and other Salesforce interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →