Best Time to Buy and Sell Stock
Reported by candidates from Oracle's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The data structure behind this one is almost embarrassingly small: a single variable holding the lowest price seen so far. Oracle reported this Best Time to Buy and Sell Stock OA in July 2026, and it's the classic one-transaction version. Buy on one day, sell on a later day, return the max profit or 0. With up to 200000 prices, you can't brute force every pair. If you've seen it, it's a two-minute job. If you blank under the timer, StealthCoder runs invisibly on your screen as a safety net and hands you the solution while you stay calm.
The problem
You are given an array prices, where prices[i] is the price of one share on day i. Choose at most one day to buy and one later day to sell. Return the maximum profit. If no profitable transaction exists, return 0. Function maxProfit(prices: int[]) → long Examples Example 1 prices = [7,1,5,3,6,4] return = 5 Buy at price 1 and sell later at price 6. Example 2 prices = [7,6,4,3,1] return = 0 Every later price is lower, so the best choice is not to trade. Constraints 1 <= prices.length <= 200000 0 <= prices[i] <= 10^9
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is one pass. Track the minimum price so far. At each day, compute price minus that minimum, and keep the largest result. Update the minimum after, or before, as long as you never sell before you buy. Start the best profit at 0 so a falling market returns 0 with no special case. That covers Example 2 with [7,6,4,3,1]. The common pitfall is the nested loop, which is O(n^2) and times out at 200000 elements. Another is taking the global max minus the global min, which ignores order. Example 1 shows it: min is 1, max is 7, but 7 comes first. Prices go up to 10^9, so the profit fits in a long, and in some languages you should use a wide type. If you freeze during the live Oracle OA, StealthCoder is the hedge that gives you this loop in real time.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Best Time to Buy and Sell Stock 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 passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as best time to buy and sell stock. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Oracle's OA.
Oracle reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Best Time to Buy and Sell Stock FAQ
How hard is Best Time to Buy and Sell Stock really?+
It's easy once you know the pattern. It's a single pass with one running minimum and one running best profit. The only real difficulty is resisting the nested loop. If you can explain why order matters, you've got the whole problem.
What's the trick for the Oracle version?+
Keep the lowest price seen so far as you scan left to right. At each day, check whether selling today beats your best profit. Initialize profit to 0 so no-trade cases return 0 automatically. That's the full solution, in O(n) time and O(1) space.
Why does the brute force fail here?+
Checking every buy and sell pair is O(n^2). With prices.length up to 200000, that's tens of billions of operations. The one-pass approach does 200000 steps. Expect large hidden test cases that punish the slow version.
What edge cases should I test?+
Test a single-element array, a strictly decreasing array like [7,6,4,3,1], and an all-equal array. All should return 0. Also test a case where the max comes before the min, like Example 1, to confirm you respect order. Use a wide integer type for safety.
How do I prepare for this in 48 hours?+
Write the one-pass solution from memory twice, then trace Example 1 by hand. Then spend your remaining time on neighboring array problems, since an OA usually has more than one question. This one shouldn't need more than a short refresher.