Magicsheet logo

Maximum Product of Three Numbers

Easy
87.8%
Updated 6/1/2025

Maximum Product of Three Numbers

1. What is this problem about?

The Maximum Product of Three Numbers interview question is a deceptive array problem. Given an integer array, find three numbers whose product is maximum and return that product.

While it seems like you should just pick the three largest numbers, the presence of negative numbers makes it more interesting. Two very large negative numbers can multiply to form a large positive number, which, when multiplied by the largest positive number, might exceed the product of the three largest positive numbers.

2. Why is this asked in interviews?

This Maximum Product of Three Numbers coding problem is extremely popular at nearly every major tech company (Apple, Microsoft, Google, Meta). It tests whether you can think beyond the most obvious case and consider edge cases (negative numbers). It also evaluates your ability to optimize from an O(NlogN)O(N \log N) sorting solution to a more efficient O(N)O(N) single-pass solution.

3. Algorithmic pattern used

The Array, Math, Sorting interview pattern is common.

  • Option 1 (Sorting): Sort the array. The answer is either max1 * max2 * max3 or min1 * min2 * max1.
  • Option 2 (Single Pass): Iterate through the array once, keeping track of the three largest and two smallest numbers found so far.

The result is max(max1 * max2 * max3, min1 * min2 * max1).

4. Example explanation

Array: [-10, -10, 1, 3, 2]

  1. Three largest: 3, 2, 1. Product = 6.
  2. Two smallest: -10, -10. Largest: 3. Product = (-10 * -10 * 3) = 300. Max product is 300.

Another example: [1, 2, 3, 4]

  1. Three largest: 4, 3, 2. Product = 24.
  2. Two smallest: 1, 2. Largest: 4. Product = (1 * 2 * 4) = 8. Max product is 24.

5. Common mistakes candidates make

A frequent mistake in the Maximum Product of Three Numbers coding problem is only considering the three largest positive numbers. Many candidates forget that the product of two negatives is positive. Another error is sorting the array and not checking the min1 * min2 * max1 case. In a high-pressure interview, forgetting to account for negative values is a common "gotcha" that interviewers look for.

6. Interview preparation tip

Always check for the "negative number" case in product optimization problems. If the problem involves products, think about how negatives cancel each other out. This pattern appears in many variations, such as finding the maximum product of two numbers or a subarray.

Similar Questions