FastPrepPeak Index in a Mountain Array

Peak Index in a Mountain Array

SambaNova Systems logoSambaNova Systems● MediumNEW GRADPHONE SCREENONSITE INTERVIEW
Learn

Problem statement

A mountain array strictly increases to one peak and then strictly decreases. Return the zero-based index of its peak.

Function

peakIndexInMountainArray(arr: int[]) → int

Examples

Example 1

arr = [0,1,0]return = 1

The middle element is the peak.

Example 2

arr = [0,2,5,3,1]return = 2

The sequence changes slope at index two.

Example 3

arr = [-5,-2,4,3]return = 2

Negative values do not change the slope logic.

Constraints

  • 3 <= arr.length <= 100000.
  • -10^9 <= arr[i] <= 10^9.
  • There is exactly one peak and it is not an endpoint.

More SambaNova Systems problems

See SambaNova Systems hiring insights
public int peakIndexInMountainArray(int[] arr) {
    // write your code here
}
arr[0,1,0]
expected1
Checking account…