Best Time to Buy and Sell Stock
Reported by candidates from Zoox's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Zoox reportedly put Best Time to Buy and Sell Stock in front of candidates in July 2025. Strip the stock story and it's one pass over an array: find the biggest gap where the smaller number comes first. No sorting, no nested loops. With up to 200000 prices, the brute force pair check will time out, so the OA is checking whether you see that. If you blank, StealthCoder runs invisibly during the live assessment and hands you the single-pass solution. Know the idea cold anyway, because it takes about five lines.
The problem
You are given an array prices, where prices[i] is the price of one share on day i. You may choose one day to buy one share and a later day to sell that share. Return the maximum profit you can earn. If no profitable transaction exists, return 0. Function maxProfit(prices: int[]) → int Examples Example 1 prices = [7,1,5,3,6,4] return = 5 Buy at price 1 and sell later at price 6, for profit 6 - 1 = 5. Example 2 prices = [7,6,4,3,1] return = 0 Every later price is lower, so no profitable transaction exists. Example 3 prices = [2,4,1] return = 2 Buy on the first day at price 2 and sell on the second day at price 4. Constraints 1 <= prices.length <= 200000 0 <= prices[i] <= 1000000000 The returned profit fits in a signed 32-bit integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is tracking the minimum price seen so far as you scan left to right. At each day, compute price minus that running minimum, and keep the best result. Start the best at 0, so a strictly falling array like [7,6,4,3,1] returns 0 without special casing. That's O(n) time and O(1) space. The common pitfall is the O(n^2) double loop, which dies at 200000 elements. The second pitfall is taking max minus min across the whole array, which ignores order. Example 3, [2,4,1], catches that: the min is 1 but it comes last, so the answer is 2. Update the minimum after computing profit or before, both work, since selling the same day gives 0. If you freeze during the live OA, StealthCoder is the hedge that surfaces this pattern fast.
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 StealthCoderThis 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 Zoox's OA.
Zoox 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 the Zoox Best Time to Buy and Sell Stock question really?+
It's an easy problem. The whole difficulty is noticing you only need a running minimum and a running best profit. If you can explain why one pass works, you're done. Most failures come from the O(n^2) approach timing out on 200000 elements.
What's the trick to solving it?+
Keep the lowest price seen so far. For each new price, compute price minus that lowest value and update the max profit. Initialize profit to 0 so the no-profit case returns 0 automatically. One loop, constant extra memory.
Why doesn't max minus min work?+
Because the buy has to come before the sell. In [2,4,1] the minimum is 1 at the end, so max minus min gives a wrong answer. The expected result is 2, from buying at 2 and selling at 4. Order matters, which is why you scan left to right.
What edge cases should I test before submitting?+
Test a single-element array, which should return 0. Test a strictly decreasing array like [7,6,4,3,1], also 0. Test the minimum appearing at the end, like [2,4,1]. Test equal prices and zeros, since prices can be 0. Values go up to 1000000000 but profit fits in 32 bits.
How do I prepare for this in 48 hours?+
Write the one-pass solution from memory twice, then trace Example 1 by hand tracking min and profit at each step. Say the complexity out loud: O(n) time, O(1) space. That's enough for this one. Spend the remaining time on similar array scan problems.