Magicsheet logo

Peak Index in a Mountain Array

Medium
48.3%
Updated 6/1/2025

Peak Index in a Mountain Array

What is this problem about?

The Peak Index in a Mountain Array problem gives you a mountain array — values strictly increase to a peak, then strictly decrease — and asks for the index of the peak element. This coding problem applies binary search to find the peak in O(log n). The array and binary search interview pattern is demonstrated.

Why is this asked in interviews?

Microsoft, Meta, Amazon, Google, and Bloomberg ask this as a standard binary search variant. It tests whether candidates can adapt binary search to "find the peak" by comparing middle elements with their neighbors to determine which half contains the peak.

Algorithmic pattern used

Binary search on slope. At any index mid: if arr[mid] < arr[mid+1], the peak is to the right (ascending slope). If arr[mid] > arr[mid+1], the peak is to the left (descending slope). Adjust lo and hi accordingly. When lo == hi, that's the peak index.

Example explanation

arr=[0,2,5,3,1]. lo=0, hi=4.

  • mid=2: arr[2]=5, arr[3]=3. 5>3 → peak is at mid or left. hi=2.
  • mid=1: arr[1]=2, arr[2]=5. 2<5 → peak is right. lo=2.
  • lo=hi=2. Return 2.

Common mistakes candidates make

  • Using arr[mid] > arr[mid-1] AND arr[mid] > arr[mid+1] (correct for finding peak but not standard binary search template).
  • Off-by-one in boundary conditions.
  • Linear scan (O(n)) when binary search (O(log n)) is expected.
  • Not handling edge peaks (though problem guarantees strict mountain).

Interview preparation tip

Mountain array peak finding is a gateway to harder binary search problems. The key: use the slope direction (arr[mid] vs arr[mid+1]) to determine which half to search. This same technique applies to "find peak in 2D matrix," "find peak with duplicates," and "search in rotated array." Practice the slope-based binary search template until it's intuitive.

Similar Questions