Reported September 2020
Bloombergarray

Best Time to Buy and Sell Stock

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

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

The mistake that sinks a first attempt on this one is grabbing the global min and global max and subtracting. Bloomberg reportedly asked Best Time to Buy and Sell Stock in September 2020, and the sell day has to come after the buy day. That one rule trips people who rush. It's a single pass over an array, no fancy structure. If you've got the OA in a day or two, you can learn this in ten minutes. And if your brain locks up mid-assessment, StealthCoder runs invisibly on screen as a safety net so you're not stuck staring at a blank editor.

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 trick is to scan once and track the lowest price seen so far. At each day, compute price minus that running minimum, and keep the best result. Start the best at 0, which handles the case where prices only fall, like [7,6,4,3,1]. The common pitfall is the global min and max subtraction. On [3,3,5,0,0,3,1,4] the max is 5 and the min is 0, but the 0 comes after the 5, so that fails. The right answer is 4. The other pitfall is a nested loop. With up to 200000 prices, O(n^2) will time out, so you need O(n) time and O(1) space. Prices go up to 10^9, and a difference fits easily in a 64-bit integer, so use a long if your language needs it. If you blank on the running-min idea during the live OA, StealthCoder can surface the one-pass solution so you can type it cleanly.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

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. If you're reading this with an OA window open, you're who this was built for.

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 Bloomberg's OA.

Bloomberg reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. 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 see it. The whole solution is one loop with two variables. Most failures come from overthinking it or ignoring the order rule. If you can explain why the running minimum works, you're done in a few minutes.

What's the trick to this problem?+

Track the minimum price seen so far as you walk the array. At every day, check whether selling today beats your best profit. Update the minimum after that check. You never need to look backward, which keeps it linear.

Why does subtracting the global min from the global max fail?+

Because the max can appear before the min. In [7,6,4,3,1] the max is first, so that subtraction gives a negative profit that can't be made. The sell must come after the buy, so the order matters, not just the values.

What should I return when prices only go down?+

Return 0. The problem says to skip trading when no profit exists. Initialize your best profit to 0 and only update it when a positive gain shows up. That covers decreasing arrays and single-element inputs automatically.

How do I prepare for this in 48 hours?+

Write the one-pass solution from memory twice, then test it on the three examples plus a single-element array. Check your handling of equal prices and large values up to 10^9. Bloomberg reportedly asked it in September 2020, so expect similar array questions.

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

OA at Bloomberg?
Invisible during screen share
Get it