Reported May 2024
Motivesorting

Maximum Product of Three Numbers

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

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

Most people fail Maximum Product of Three Numbers the same way: they grab the three biggest values and move on. Motive reported this one in May 2024, and the example is built to punish exactly that shortcut. Two negatives and one positive beat three positives when the negatives are large enough in magnitude. The pattern is sorting or a single pass tracking extremes. If you've got an OA invite and 48 hours, this is a ten-minute problem once you see the trap. StealthCoder sits invisibly as a safety net on the live OA if you blank on the edge case.

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 answer is either the product of the three largest numbers, or the product of the two smallest (most negative) numbers and the largest. Compare those two and return the max. Sorting gives you this in O(n log n). A single pass tracking top three and bottom two gets O(n) with O(1) space. The common pitfall is overflow. Values fit in 32-bit, but a product of three can reach about 10^27? No, 2^31 cubed is roughly 9.9e27, which exceeds 64 bits, so check the stated constraints and cast to long before multiplying, never after. Another miss is forgetting all-negative arrays, where the three largest are the least negative, and that case is already covered by the top-three product. If you freeze on any of this during the live OA, StealthCoder is there as a hedge, reading the problem and handing you the working solution.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as maximum product of three numbers. If you have time before the OA, drill that.

⏵ The honest play

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

Motive reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Maximum Product of Three Numbers FAQ

What's the trick in Maximum Product of Three Numbers?+

Two negatives multiply into a positive. So the best product is either the three largest values or the two smallest values times the largest. Compute both candidates and return the bigger one. That single comparison handles every case.

Do I need to sort the array?+

No. Sorting works and is easy at O(n log n), which is fine for 10^5 elements. A single pass tracking the three largest and two smallest is O(n) and uses constant space. Either should pass, so pick the one you can write without bugs.

How do I avoid overflow here?+

Cast to long before you multiply. Multiplying ints and then widening the result already overflowed. Write (long) a * b * c so each step happens in 64-bit. The function returns a long for exactly this reason.

What edge cases should I test?+

Test an all-negative array, an array with zeros, exactly three elements, and the sample with two big negatives. All-negative is the sneaky one: the answer is the three values closest to zero, which the top-three product already gives you.

How hard is this really for the Motive OA?+

It's easy once you know the negative-number twist. Reported in May 2024, it's a quick problem that tests whether you consider sign. Spend your 48 hours on edge-case habits like casting and sign checks, not on advanced algorithms.

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

OA at Motive?
Invisible during screen share
Get it