Reported December 2024
Amazongreedy

Calculate Max Profit

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

Amazon's "Calculate Max Profit" showed up in December 2024 OAs, and it's a greedy problem disguised as a simple optimization. You're given a sequence of prices or transactions, and you need to find the maximum profit you can extract. The trap is thinking you need to try every combination. You don't. There's a pattern that lets you scan left-to-right and decide on the fly. StealthCoder will spot it instantly if you freeze mid-assessment, but you should know the trick going in.

Pattern and pitfall

The greedy move is usually: track the minimum price seen so far, then at each step calculate the profit if you sold at the current price. Keep the max. Or, if it's a multi-transaction version, buy whenever you see an upward slope and sell at the peak. The common miss is thinking you need dynamic programming or that you can only buy and sell once. Greedy works because you're never worse off taking a profit when the price rises the next day. If you blank on whether to optimize for one trade or many, re-read the problem statement carefully. StealthCoder catches that nuance in seconds and delivers the right solution shape.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Calculate Max Profit 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as best time to buy and sell stock. If you have time before the OA, drill that.

⏵ The honest play

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

Amazon reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Calculate Max Profit FAQ

Is this the classic 'best time to buy and sell stock' problem?+

Probably. Amazon loves this one in various flavors: one transaction, unlimited, or with cooldown. The core insight is the same: greedy beats brute force. If the problem says you can make multiple transactions, that's a flag to buy-and-sell on every upswing.

How do I know if it's one transaction or multiple?+

Read the problem statement word by word. 'Single transaction' or 'at most one' means you track min and max in one pass. 'Multiple transactions' or 'unlimited' means you sum all positive differences between consecutive days. The difference in code is small but critical.

What's the time complexity I need?+

Greedy solves it in O(n) time, O(1) space. If you're writing nested loops or a DP table, you're overcomplicating. A single pass with a running min and max profit is the right shape.

Will there be edge cases like negative prices or empty arrays?+

Usually the problem constrains prices to positive integers and guarantees at least one price. But always check: if the array is empty or has one element, return 0. If all prices fall, profit is 0. Handle those before the main loop.

How do I prepare for this in 48 hours?+

Know the one-transaction and multiple-transaction variants cold. Trace through a short example by hand. Then code it without looking at solutions. The muscle memory matters more than rereading. If you get stuck live, StealthCoder has you covered.

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