Reported March 2026
Retooldynamic programming

Maximum Profit with Exactly Two Stock Transactions

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

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

The Retool OA reported in March 2026 looks like the classic two-transaction stock problem, but one word changes it: exactly. You can't skip a trade, so a falling market forces you to take losses. Strip the story and it's a split-point problem. Best single trade on the left, best single trade on the right, add them. If you're taking this in the next day or two, learn the one-pass state version cold. StealthCoder is the safety net on the live OA if the edge cases make you blank mid-problem.

The problem

Given daily stock prices, complete exactly two non-overlapping transactions. Each transaction buys once and sells on a later day, and the first sale must occur before the second purchase.
Return the maximum sum of the two profits. Both transactions are required, so either one may lose money.

Function
maxProfitExactlyTwo(prices: int[]) → int

Examples
Example 1
prices = [3,3,5,0,0,3,1,4]
return = 6
Trade from 3 to 5, then from 0 to 4.
Example 2
prices = [7,6,4,3,1]
return = -2
Two required adjacent one-point losses are optimal.
Example 3
prices = [1,2,3,4]
return = 2
Use two one-point gains: 1 to 2 and 3 to 4.

Constraints
4 <= prices.length <= 100000.
0 <= prices[i] <= 1000000.
The answer fits in a signed 32-bit integer.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The problem reduces to four running states: best value after buy 1, after sell 1, after buy 2, after sell 2. Scan prices once and update each state from the previous one. The twist is the exactly-two rule. In the usual version, sell1 can start at 0 because skipping a trade is free. Here it can't. Initialize sell1, buy2 and sell2 to negative infinity so a state only counts once a real transaction has happened. Example 2 shows why: [7,6,4,3,1] returns -2, two adjacent one-point losses. Another pitfall is requiring the second buy to come strictly after the first sell, so you can't reuse a day. With 100000 prices you need O(n) time and O(1) space. Watch for overflow on sentinels. If the state logic slips under pressure, StealthCoder can hand you the working version on the live OA.

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 Profit with Exactly Two Stock 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

⏵ The honest play

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

Retool 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 Profit with Exactly Two Stock Transactions FAQ

What's the trick in the Retool two-transaction stock problem?+

Track four states in one pass: after first buy, first sell, second buy, second sell. Each state is the max of keeping it or moving from the previous state using today's price. It's O(n) time and O(1) space. The only change from the classic is the initialization.

How is 'exactly two' different from 'at most two'?+

At most two lets you skip trades, so profit never goes below zero. Exactly two forces both trades. Initialize sell1, buy2 and sell2 to negative infinity instead of zero. Then [7,6,4,3,1] correctly returns -2 instead of 0.

Can I solve it with a prefix and suffix approach?+

Yes. Compute the best single-trade profit ending by day i going left to right, and starting after day i going right to left, then combine at a split. For exactly two, each side needs at least one real trade, so a loss is allowed. The state machine is shorter to code.

Why does example 3 return 2 and not 3?+

With [1,2,3,4], one long trade would earn 3, but you must make two. Splitting into 1 to 2 and 3 to 4 gives 2. Buying and selling on the same day isn't allowed, and the second buy must come after the first sell, so you lose a day to the gap.

How do I prepare for this in 48 hours?+

Write the four-variable solution from memory twice. Then test it on the three examples, especially the all-decreasing one. Check your initial values and update order. Make sure sell2 reads the old buy2 or the updated one consistently, since same-day updates can hide bugs.

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

OA at Retool?
Invisible during screen share
Get it