Best Time to Buy and Sell Stock
Reported by candidates from Motive's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Motive reported this one in December 2025, and it's the classic Best Time to Buy and Sell Stock. Strip the story and it's one question: for every day, what's the cheapest price you've seen before it? That's a single pass over the array, nothing fancier. If you're taking the Motive OA soon, expect this to be a warm-up where speed and clean edge cases matter more than cleverness. The risk isn't the idea. It's overthinking it into a nested loop, or blanking on the return-0 case. StealthCoder sits invisibly on your screen during the live OA as a safety net if your mind goes empty.
The problem
You are given an integer 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 for profit 5. Example 2 prices = [7,6,4,3,1] return = 0 Every later price is lower, so the best choice is not to trade. Example 3 prices = [3,3,5,0,0,3,1,4] return = 4 Buy at a price of 0 and sell later at 4. Constraints 1 <= prices.length <= 200000 0 <= prices[i] <= 10^9
Reported by candidates. Source: FastPrep
Pattern and pitfall
The pattern is a running minimum. Walk the array once, track the lowest price so far, and at each day compute price minus that minimum. Keep the best difference seen. Start the best at 0, so a falling market returns 0 with no extra branch. The pitfall is the brute force double loop. With up to 200000 prices, O(n^2) will time out, and the Motive test cases will likely include large inputs. Another trap is updating the minimum after computing profit versus before. Either order works if you handle the same-day case, since profit there is 0. Values reach 10^9, and the return type is long, so use a 64-bit type where your language needs it. Differences fit anyway, but match the signature. If you freeze on the live OA, StealthCoder is the hedge that hands you the one-pass solution.
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 Motive's OA.
Motive 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. The whole solution is one loop with two variables: the minimum price so far and the best profit so far. Most people who miss it either brute force with nested loops or forget that the answer floors at 0. If you've seen it once, you can write it in under five minutes.
What's the trick to solving it in O(n)?+
Don't compare every pair. At each day, the best buy is the cheapest earlier price, so keep that as a running minimum. Profit for the day is current price minus the minimum. Track the maximum of those values. One pass, constant extra space.
Why does my solution return negative numbers?+
You initialized the best profit to something like negative infinity or the first difference. Start it at 0 instead. The problem says to return 0 if no profitable trade exists, so a strictly decreasing array like [7,6,4,3,1] should give 0 without any special casing.
Do I need a long for the result?+
The signature returns long, so match it. Prices go up to 10^9, and a single difference fits in a 32-bit int, but using 64-bit costs nothing and avoids surprises. In Python it doesn't matter. In Java or C++, declare the profit variable as long.
How should I prepare in 48 hours for the Motive OA?+
Write this one from memory twice, then test it on the three examples, including the all-decreasing case and a single-element array. After that, skim other array one-pass problems like running max or prefix sums. Don't grind new topics. Focus on clean code and edge cases.