Reported September 2026
Amazondynamic programming

Maximum Stock Profit with At Most Two Transactions

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 reported this one in September 2026, and the first thing you'll notice is the cap in the statement: at most two transactions, never overlapping, and prices.length can hit 10^5. That size rules out anything quadratic. It's the classic two-transaction stock problem, and it's a DP with four running states. If you've seen it, it's five minutes of typing. If you haven't, it's easy to overthink. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but the pattern below should be enough to walk in ready.

The problem

You are given an array prices, where prices[i] is the price of one stock on day i. Return the maximum profit you can earn by completing at most two transactions.
A transaction consists of buying one share and selling that share on a later day. You may hold at most one share at a time, so a second transaction can begin only after the first transaction has been sold. You may also complete fewer than two transactions.

Function
maxProfitAtMostTwo(prices: int[]) → int

Examples
Example 1
prices = [3,3,5,0,0,3,1,4]
return = 6
Buy at 0 and sell at 3, then buy at 1 and sell at 4. The total profit is 3 + 3 = 6.
Example 2
prices = [1,2,3,4,5]
return = 4
One transaction from price 1 to price 5 earns the maximum profit of 4.
Example 3
prices = [7,6,4,3,1]
return = 0
No later price exceeds an earlier price, so the best choice is to make no transaction.

Constraints
1 <= prices.length <= 10^5
0 <= prices[i] <= 10^5
A buy must occur before its matching sell.
Transactions may not overlap.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is to track four values as you scan prices once: buy1 (best balance after the first buy), sell1 (best profit after the first sell), buy2 (best balance after the second buy, using sell1's profit), and sell2 (best profit after the second sell). Initialize buy1 and buy2 to negative infinity, and sell1 and sell2 to 0. For each price: buy1 = max(buy1, -p), sell1 = max(sell1, buy1 + p), buy2 = max(buy2, sell1 - p), sell2 = max(sell2, buy2 + p). Return sell2. That's O(n) time and O(1) space. The common pitfall is updating the states in the wrong order or starting buy2 at 0, which lets it cheat. Another is greedily taking the two biggest gaps, which breaks when they overlap. Example 3 returns 0, so make sure your zero start handles the no-trade case. If you freeze on the live OA, StealthCoder can hand you this recurrence while you keep typing.

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 Stock Profit with At Most Two Transactions 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

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as best time to buy and sell stock iii. 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. 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 Stock Profit with At Most Two Transactions FAQ

How hard is the Amazon two-transaction stock problem really?+

It's a hard-rated problem, but it's well known and the solution is short. Once you see the four-state DP (buy1, sell1, buy2, sell2), the code is about eight lines. The difficulty is in finding the states, not in the implementation.

What's the trick to solving it in O(n)?+

Scan prices once and keep four variables for the best outcome after each stage: first buy, first sell, second buy, second sell. Each stage builds on the previous one, so the second buy uses the first sell's profit. No nested loops are needed.

Why doesn't greedily picking the two biggest price rises work?+

The two largest gaps can overlap or share days, and the problem forbids holding two shares at once. For example, one long rise might beat two separate small ones, or split better into two. The DP handles both cases automatically.

What edge cases should I test before submitting?+

Test a strictly decreasing array like [7,6,4,3,1], which must return 0. Test a single element, a strictly increasing array where one transaction wins (answer 4 for [1,2,3,4,5]), and the sample [3,3,5,0,0,3,1,4] returning 6.

How do I prepare for this in 48 hours?+

Write the four-variable solution from memory twice, then run it by hand on the three examples. Also be ready for the at-most-k variant, since the same idea generalizes with arrays of states. Spend the rest of your time on other patterns.

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