Reported July 2026
Amazonmonotonic stack

Maximum Sum of Heights

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

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

The mistake that sinks a first attempt on this Amazon OA, reported in July 2026, is trying every peak and rebuilding the mountain from scratch each time. That's O(n^2), and it's fine until the input gets big. Maximum Sum of Heights hands you a maxHeights array and asks for the largest mountain sum where every height stays under its cap. The real pattern is a monotonic stack with prefix and suffix sums. If you blank on the live OA, StealthCoder runs invisibly on your desktop as a safety net and gives you the approach while you type.

The problem

Given positive maximum heights maxHeights, choose a positive height for every index so that height[i] <= maxHeights[i]. The resulting array must be mountain-shaped: for some peak index, heights do not decrease before the peak and do not increase after it.
Return the maximum possible sum of the chosen heights.
Interview follow-up
The interviewer asked follow-up questions about the approach, complexity, and possible alternatives.

Function
maximumSumOfHeights(maxHeights: int[]) → long

Examples
Example 1
maxHeights = [5,3,4,1,1]
return = 13
Choosing [5,3,3,1,1] forms a mountain with sum 13.
Example 2
maxHeights = [6,5,3,9,2,7]
return = 22

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: for a fixed peak, each side is a running minimum of the caps as you move away from the peak. Brute force recomputes that for every peak, which is O(n^2). Instead, compute left[i], the best sum of a non-decreasing arrangement ending at i, using a monotonic stack. Pop while the stack top's cap is greater than maxHeights[i]. Then left[i] = left[j] + maxHeights[i] * (i - j), where j is the new top, or the whole prefix if the stack is empty. Do the same from the right. The answer is the max of left[i] + right[i] - maxHeights[i]. Common pitfalls: overflow, so use a 64-bit type, and double counting the peak. Check example 1: peak at index 0 gives 5+3+3+1+1 = 13. StealthCoder is your hedge if the stack recurrence escapes you under pressure.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Maximum Sum of Heights 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ The honest play

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

Amazon reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Maximum Sum of Heights FAQ

What's the trick for Maximum Sum of Heights?+

Treat each index as the peak and note that each side is a running minimum of caps. A monotonic stack lets you compute the best left sum and best right sum for every index in O(n) total, then combine them.

Is the O(n^2) brute force acceptable?+

It works for small inputs and is a good first pass to verify your logic. For each peak, expand outward with a running minimum. If the OA has large n, it will time out, so know the stack version as the real answer.

Why do I subtract maxHeights[i] at the end?+

Both left[i] and right[i] include the peak's height. Adding them counts the peak twice, so you subtract it once. Forgetting this is the classic off-by-one-peak bug that fails the examples.

Do I need a long type?+

Yes. The return type is long for a reason. Heights multiplied by lengths can exceed 32-bit integer range, so keep your prefix arrays and sums in 64-bit to avoid silent overflow.

How do I prepare for this in 48 hours?+

Do a few monotonic stack problems like next smaller element and largest rectangle in histogram. Then write this one from memory, tracing example 2 by hand. The answer 22 is a good check that your left and right arrays are correct.

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

OA at Amazon?
Invisible during screen share
Get it