Reported September 2026
Googlebinary search

First and Last Target Position in a Mountain Array

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

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

Google flagged this one in September 2026, and it looks scarier than it is. Strip the mountain story and it's three binary searches glued together: find the peak, search the rising side, search the falling side. The O(log n) requirement is the whole point, so a linear scan fails even if it passes the examples. If you've got the OA in a day or two, this is a pattern you can lock in fast. And if your brain freezes mid-assessment, StealthCoder sits invisibly on your screen as a safety net and hands you the structure when you need it.

The problem

You are given an integer array mountain that strictly increases to one peak and then strictly decreases, plus an integer target. The peak is not an endpoint.
Because the two slopes may contain the same value, target can appear once on each side of the peak. Return [first, last], the first and last index where target occurs. If the target is absent, return [-1, -1].
Your algorithm must run in O(log n) time.

Function
searchMountainRange(mountain: int[], target: int) → int[]

Examples
Example 1
mountain = [1,3,5,7,5,3,1]
target = 3
return = [1,5]
The target appears at index 1 on the increasing slope and index 5 on the decreasing slope.
Example 2
mountain = [-5,-2,4,9,6,1]
target = 9
return = [3,3]
The target is the peak, so its first and last positions are both 3.
Example 3
mountain = [0,2,8,6,4]
target = 5
return = [-1,-1]
The target does not appear on either slope.

Constraints
3 <= mountain.length <= 100000.
-10^9 <= mountain[i], target <= 10^9.
There is exactly one non-endpoint peak.
The left slope is strictly increasing and the right slope is strictly decreasing.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The reduction: binary search for the peak by comparing mountain[mid] to mountain[mid+1]. If mid is smaller than the next, the peak is to the right, otherwise it's at mid or left. Then run a standard ascending binary search on [0, peak] and a descending binary search on [peak+1, n-1]. Because slopes are strictly monotonic, each side has at most one match. So no duplicate-range expansion is needed. First is the left hit if it exists, otherwise the right hit. Last is the right hit if it exists, otherwise the left hit. The common pitfall is flipping the comparison on the descending side, or searching the peak twice and mishandling the case where target equals the peak. Another trap is off-by-one on the peak loop with lo < hi. If you blank on the descending comparison during the live OA, StealthCoder is the hedge that gets you unstuck.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill First and Last Target Position 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. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

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

⏵ The honest play

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

Google reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

First and Last Target Position in a Mountain Array FAQ

What's the trick in this Google mountain array problem?+

Three binary searches. First locate the peak by comparing mid with mid+1. Then do an ascending search on the left part and a descending search on the right part. Combine the hits into [first, last]. Each step is O(log n), so the total stays O(log n).

Can target appear more than twice?+

No. Each slope is strictly monotonic, so a value shows up at most once per side. The peak belongs to one search only, so if target equals the peak you get the same index for first and last, like [3,3] in Example 2.

How do I decide first versus last?+

Run the left search and the right search. If the left one finds an index, that's first, since left indices are smaller. If only the right finds it, first is that index. Last is the right result if found, otherwise the left one. Neither found means [-1,-1].

Why does a linear scan not work here?+

The problem demands O(log n) and n can reach 100000. A scan technically returns correct answers, but it breaks the stated complexity requirement and may fail hidden checks or get flagged. Use binary search for the peak and both slopes.

How do I prepare for this in 48 hours?+

Write a peak finder with lo < hi and mid vs mid+1. Then write an ascending binary search and flip it for descending. Test on the three examples, including the peak-as-target and missing-target cases. Edge cases matter more than volume of practice.

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

OA at Google?
Invisible during screen share
Get it