Skip to content
Decoder

Peak Index in a Mountain Array

Medium

Problem

A mountain array strictly increases up to a single peak and then strictly decreases. It has at least three elements, and the peak is never at either end. Given a mountain array arr, return the index of the peak in O(log n) time.

Example 1

Input
arr = [0, 1, 0]
Output
1

Example 2

Input
arr = [0, 2, 1, 0]
Output
1

Example 3

Input
arr = [3, 4, 5, 1]
Output
2

Constraints

  • 3 <= arr.length <= 10^5
  • 0 <= arr[i] <= 10^6
  • arr is guaranteed to be a mountain array.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.