Reported September 2026
FlexTradearray

Best Time to Buy and Sell Stock

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

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

The FlexTrade OA reported in September 2026 hands you Best Time to Buy and Sell Stock, and the trap isn't the idea, it's the edge case. A naive pair of nested loops looks right on the examples, then dies on an array of 200000 prices. Descending prices also have to return 0, not a negative number. It's a single-pass array problem, and you can write it in five lines if you stay calm. If you blank mid-assessment, StealthCoder runs invisibly on your desktop as a safety net and gives you the solution live. Here's the trick so you don't need it.

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

Track two things as you scan left to right: the lowest price seen so far and the best profit so far. For each price, compute price minus the running minimum, update the best profit if it's larger, then update the minimum. Start the profit at 0, so a strictly falling array like [7,6,4,3,1] returns 0 with no special case. The classic pitfall is the O(n^2) brute force, which times out at 200000 elements. The second pitfall is selling before buying, which happens if you update the minimum before computing profit on the same day or search for the global max and min separately. Example 3 shows this: the max is 5 but it comes before the 0. Use a long for the profit since prices reach 10^9. If the live OA freezes you, StealthCoder is the hedge, but this one is a ten-minute rewrite.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

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 would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as best time to buy and sell stock. If you have time before the OA, drill that.

⏵ The honest play

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

FlexTrade 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.

Best Time to Buy and Sell Stock FAQ

What's the trick to Best Time to Buy and Sell Stock?+

Keep a running minimum price and a running best profit in one pass. At each day, profit is the current price minus the minimum so far. Update the best, then update the minimum. It's O(n) time and O(1) space, and it handles the no-profit case by starting at 0.

Why does the naive solution fail on the FlexTrade version?+

Nested loops are O(n^2), and the constraints allow 200000 prices. That's tens of billions of pair checks, so it times out. A second naive mistake is subtracting the global min from the global max, which ignores that you must buy before you sell.

What should I return when prices only go down?+

Return 0. Initialize your best profit to 0 and only raise it when a positive gain shows up. Example 2, [7,6,4,3,1], is exactly this case. Never return a negative number, since not trading is allowed.

Do I need a long for the result?+

The function signature returns a long, so use a 64-bit type in languages where it matters. Prices go up to 10^9, and a single difference fits in 32 bits, but matching the signature avoids any overflow worries and type errors.

How do I prepare for this in 48 hours?+

Write the one-pass solution from memory three times, then test it on the three examples plus a single-element array. A length of 1 should return 0. Also try all equal prices. If you can explain why the minimum is updated after the profit check, you're ready.

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

OA at FlexTrade?
Invisible during screen share
Get it