Peak Index in a Mountain Array

EasyBinary Searchbinary search

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given an integer array arr that is guaranteed to be a **mountain**: its length is at least 3, it strictly increases up to a single peak, and then strictly decreases to the end. The peak is never the first or last element. Return the index of the peak (the largest element). Solve it in O(log n) time.

Examples

Input: arr = [0,1,0]
Output: 1
1 at index 1 is the peak.
Input: arr = [0,10,5,2]
Output: 1
10 is the largest element.
Input: arr = [3,5,3,2,0]
Output: 1
The array rises only once, to 5 at index 1.

Constraints

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

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More binary search problems