Reported October 2020
SambaNova Systemsbinary search

Peak Index in a Mountain Array

Reported by candidates from SambaNova Systems's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live SambaNova Systems OA. Under 2s to a working solution.
Founder's read

Peak Index in a Mountain Array looks like a warm-up, and it is. SambaNova Systems candidates reported it in October 2020, and it boils down to one question: where does the slope flip from up to down? You can scan for it in O(n), but with up to 100000 elements the real ask is binary search. If your OA invite lands in the next day or two, this is the pattern to lock in. And if your brain freezes mid-assessment, StealthCoder runs invisibly on your desktop and hands you the solution as a safety net.

The problem

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.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: the array is strictly increasing then strictly decreasing, so compare arr[mid] with arr[mid+1]. If arr[mid] < arr[mid+1], you're on the uphill side, so the peak is to the right and you set lo = mid + 1. Otherwise you're on the downhill side or at the peak, so set hi = mid. Loop while lo < hi and return lo. That's O(log n) time and O(1) space. The common pitfall is using mid-1 and mid+1 checks and blowing past the bounds, or using a linear scan and getting dinged on efficiency. Another miss is writing lo <= hi with hi = mid, which loops forever. The constraints guarantee exactly one peak and never at an endpoint, so you don't need edge-case branches. If you blank on the loop invariant during the live OA, StealthCoder is the hedge that gives you the clean version.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Peak Index in a Mountain Array cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as peak index in a mountain array. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass SambaNova Systems's OA.

SambaNova Systems reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Peak Index in a Mountain Array FAQ

What's the trick for Peak Index in a Mountain Array?+

Binary search on the slope. Compare arr[mid] to arr[mid+1]. If it's rising, move lo to mid+1. If it's falling, move hi to mid. When lo meets hi, that index is the peak. No need to check the left neighbor.

Is a linear scan acceptable?+

It works for correctness, since n is at most 100000, but it's O(n). Interviewers at a company like SambaNova usually expect the O(log n) binary search. Write the binary search so you don't lose points on efficiency.

How hard is this problem really?+

Easy to medium. The idea is simple, but off-by-one errors in the binary search bounds trip people up. Use lo < hi with hi = mid and you avoid infinite loops and out-of-range reads.

Do negative values or duplicates break the approach?+

No. Values range from -10^9 to 10^9, but only comparisons matter, so negatives change nothing. The mountain is strictly increasing then strictly decreasing, so adjacent equal values don't occur and the slope test stays clean.

How do I prepare for this in 48 hours?+

Write the binary search from memory three times on different arrays, including [0,1,0] and [-5,-2,4,3]. Then do a couple of other binary-search-on-condition problems. The pattern is what matters, not the specific problem.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with SambaNova Systems.

OA at SambaNova Systems?
Invisible during screen share
Get it