Interview Questions/Coding/Single Peak Locator

Single Peak Locator

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Single Peak Locator

A mountain scanner records heights along a trail. The trail has exactly one highest point, called the peak. Before the peak, heights rise, and after the peak, heights fall. Given the height array, return the index of the peak element. The index is 0-based. Give answer in log(n) time

Examples

Example 1
Input:
1 3 2
Output:
1

Explanation: Height 3 is greater than both neighbors, so its index is 1.

Approach hint

If heights[mid] is less than heights[mid + 1], the peak is to the right.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp