Maximum Profit with Exactly One Stock Transaction
Reported by candidates from Retool's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Retool reported this one in March 2026, and it looks like the classic stock problem until the second paragraph bites. You must trade exactly once, so the answer can go negative. Most people paste the usual solution that starts the best profit at zero, and it quietly returns 0 on a falling price list when it should return -1. That's the whole trap. If you've got an OA coming, know this before you open the editor. And if you blank under the clock, StealthCoder runs invisibly on your screen as a safety net while you work it out.
The problem
Given daily stock prices, choose exactly one buy day and one later sell day. Return the greatest possible profit sellPrice - buyPrice. You must complete the transaction even when every choice loses money, so the answer may be negative. Function maxProfitExactlyOne(prices: int[]) → int Examples Example 1 prices = [7,1,5,3,6,4] return = 5 Buy at 1 and sell later at 6. Example 2 prices = [7,6,4,3,1] return = -1 The least costly required trade buys at 7? No: buying at 4 and selling at 3, or buying at 3 and selling at 1, loses at least 1; the best legal loss is -1. Example 3 prices = [3,3] return = 0 Buying and selling on the two distinct days produces zero profit. Constraints 2 <= 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 pattern is a single pass over the array, tracking the minimum price seen so far. For each day i starting at index 1, compute prices[i] minus that running minimum, and keep the max of those. The trick is the initial value. Don't start best at 0. Start it at prices[1] - prices[0], the first legal trade, then update the minimum as you go. Check example 2: [7,6,4,3,1] gives differences of -1, -2, -1, -2 against the running minimum, so the best is -1. Example 3 returns 0 because equal prices are a legal trade. The loop must update the minimum after computing the profit, so you never sell on the same day you buy. That's O(n) time and O(1) space. If the live OA freezes you, StealthCoder can hand you this logic so you can sanity-check it against the three examples.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Maximum Profit with Exactly One Stock Transaction 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 by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Retool's OA.
Retool reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Maximum Profit with Exactly One Stock Transaction FAQ
What's the trick in the Retool exactly-one-transaction problem?+
Initialize the best profit to the first legal trade, prices[1] - prices[0], not zero. Then scan from index 1, tracking the running minimum of earlier days. Compute profit before updating the minimum so you never buy and sell on the same day.
Why does the usual stock solution fail here?+
The classic version lets you skip trading, so it starts best at 0 and never goes negative. This one forces a trade. On [7,6,4,3,1] the classic code returns 0, but the correct answer is -1.
How hard is this really?+
Easy if you spot the forced-trade rule, medium-feeling if you don't. The algorithm is one loop. Most failures come from the initialization and from updating the minimum in the wrong order, not from the idea.
What edge cases should I test before submitting?+
Test a strictly decreasing array like [7,6,4,3,1], which expects -1. Test two equal prices like [3,3], which expects 0. Test the minimum length of 2. Also test a case where the lowest price is on the last day and can't be used to buy.
How do I prepare in 48 hours?+
Write the one-pass solution from memory twice, then run all three examples by hand. Focus on the initialization and the order of updates. Skip fancy approaches. With n up to 100000, O(n) is the target and brute force O(n^2) will be too slow.