Find Peak Element

MediumBinary 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 a **mountain-shaped** integer array nums: its values strictly increase up to a single highest element and then strictly decrease. Either side may be empty, so a strictly increasing or strictly decreasing array also counts. That highest element is the array's only **peak**, meaning it is strictly greater than each neighbour that exists. Return the index of the peak. Your solution must run in O(log n) time.

Examples

Input: nums = [1,3,5,4,2]
Output: 2
nums[2] = 5 is larger than both neighbours.
Input: nums = [1,2,3]
Output: 2
The array only rises, so the last element is the peak.
Input: nums = [9,4,-1]
Output: 0
The array only falls, so the first element is the peak.

Constraints

  • 1 <= nums.length <= 1000
  • -2^31 <= nums[i] <= 2^31 - 1
  • `nums` strictly increases and then strictly decreases (either part may be empty), so exactly one peak exists.

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