Maximum Product of Three Numbers
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Two negatives and a 3 give you 300 in the example, and that's the whole puzzle in one line. Bloomberg reported this one in July 2022: pick three distinct indices from nums and return the largest product as a signed 64-bit integer. It looks like a sorting problem, and it is, but the trap is the sign. If you only grab the three biggest numbers, you'll fail the negative cases. If you've got an invite and 48 hours, this is a ten-minute fix once you see it. StealthCoder sits invisibly on your screen during the live OA as a safety net if your mind goes blank on the edge cases.
The problem
Choose exactly three distinct indices from nums. Return the maximum product of the three selected values as a signed 64-bit integer. Function maximumProductOfThree(nums: int[]) → long Examples Example 1 nums = [-10,-10,1,3,2] return = 300 The two negative values and 3 produce 300. Constraints 3 <= nums.length <= 10^5. Values fit in signed 32-bit integers.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: the best product is either the three largest values, or the two smallest values (the most negative) times the largest. Two negatives multiply into a positive, so that pair can beat the second and third largest positives. Sort the array and compare nums[n-1]*nums[n-2]*nums[n-3] against nums[0]*nums[1]*nums[n-1]. Sorting is O(n log n), which is fine for 10^5. You can do it in O(n) by tracking the top three and bottom two in one pass. The common pitfall is overflow. Values fit in 32-bit, but a product of three can reach about 10^27... actually it's bounded near 2^93 in theory, so check what your language does and cast to long before multiplying. Another miss is arrays of all negatives, where the three largest are the right answer. StealthCoder is the hedge during the live OA if you blank on the sign case or the cast.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Maximum Product of Three Numbers 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as maximum product of three numbers. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Maximum Product of Three Numbers FAQ
What's the trick to Maximum Product of Three Numbers?+
Compare two candidates: the product of the three largest numbers, and the product of the two smallest numbers times the largest. Negatives flip sign, so two big negatives can beat positives. Return the larger of the two. That covers every sign mix.
How hard is this one really for the Bloomberg OA?+
Easy once you've seen the negative case, and a trap if you haven't. Most failures come from only taking the top three values. The code is about five lines after sorting. Test it against the [-10,-10,1,3,2] example before submitting.
Do I need O(n) or is sorting fine?+
Sorting is fine for n up to 10^5. O(n log n) runs comfortably at that size. The O(n) version tracks three maximums and two minimums in one pass. It's a nice touch but not required to pass.
Where do overflow bugs show up here?+
The return type is a signed 64-bit integer, so cast the values to long before you multiply. Multiplying two ints in int arithmetic overflows before the widening happens. Write the cast on the first operand, then multiply the rest.
How do I prepare for this in 48 hours?+
Write the sort solution from memory, then the single-pass version. Test all negatives, all positives, mixed signs, and zeros. Also check the minimum length of 3. That's about 30 minutes and it covers the whole problem.