# The Mountain Peak

> The sequence has a summit.

Canonical URL: <https://datadriven.io/problems/the_mountain_peak>

Domain: Python · Difficulty: medium · Seniority: L4

## Problem

Given a non-empty list of integers, return the index of any peak element - one strictly greater than both its neighbors. Treat the outside of the array as negative infinity. Algorithm must run in O(log n).

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **binary search on unsorted data**, specifically the peak-finding variant. Interviewers use it to check whether candidates can apply binary search beyond simple sorted-array lookups by reasoning about local gradient information.

> **Trick to Solving**
>
> At any index, if the element is smaller than its right neighbor, a peak must exist to the right (because the boundary is negative infinity). If smaller than the left neighbor, a peak exists to the left. This lets you halve the search space each step.

---

### Break down the requirements

#### Step 1: Set up binary search boundaries

`left` starts at 0, `right` at `len(nums) - 1`.

#### Step 2: Compare the midpoint with its right neighbor

If `nums[mid] < nums[mid + 1]`, a peak lies to the right. Otherwise, `mid` itself could be a peak, so search left (including mid).

#### Step 3: Terminate when left equals right

At that point, `left` is a peak index.

---

### The solution

**Binary search on local gradient**

```python
def find_peak(nums):
    left = 0
    right = len(nums) - 1
    while left < right:
        mid = (left + right) // 2
        if nums[mid] < nums[mid + 1]:
            left = mid + 1
        else:
            right = mid
    return left
```

> **Time and Space Complexity**
>
> **Time:** O(log n). The search space halves each iteration.
> 
> **Space:** O(1). Only three integer variables.

> **Interviewers Watch For**
>
> Can you prove correctness? The invariant is that a peak always exists in `[left, right]`. When `nums[mid] < nums[mid+1]`, moving left to `mid+1` preserves this because the sequence must eventually decrease (or hit the boundary).

> **Common Pitfall**
>
> Using `right = mid - 1` instead of `right = mid`. When `nums[mid] >= nums[mid+1]`, mid itself might be the peak, so excluding it is incorrect.

---

## Common follow-up questions

- What if the array could have plateaus (consecutive equal values)? _(Tests that the strict 'greater than neighbors' guarantee is needed for O(log n).)_
- How would you find all peaks instead of just one? _(Tests that finding all peaks requires O(n) since you cannot skip candidates.)_
- What if this were a 2D grid and you needed a peak cell? _(Tests extending binary search to 2D with column-max reduction.)_
- Why does this problem not require the array to be sorted? _(Tests understanding that gradient information alone suffices for binary search.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_mountain_peak)
- [Python Interview Questions](https://datadriven.io/python-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.