628. Maximum Product of Three Numbers
Asked at Salesforce
Difficulty
Easy
Time
O(n log n)
Space
O(1)
Pattern
Array
Companies
1
Problem
You are given an integer array nums. Pick three different positions in the array and multiply the three values at those positions. Return the largest product you can get this way.
The tricky part is negative numbers. Two negatives multiplied together give a positive, so a pair of very negative numbers can beat the three biggest values. For example, in [-10, -10, 1, 2, 3], the best choice is -10 * -10 * 3 = 300, not 1 * 2 * 3 = 6.
So there are only two candidates worth checking: the three largest values, and the two smallest values together with the largest value. The answer is the bigger of those two products.
Two candidates, not one
Constraints
- ●
3 <= nums.length <= 10^4 - ●
-1000 <= nums[i] <= 1000
Examples
nums = [1,2,3]6nums = [1,2,3,4]24nums = [-1,-2,-3]-6Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Salesforce | Easy | View all Salesforce questions → |
How to Think About It
Brute force: check every triplet with three nested loops — O(n^3).
Sort the array first and consider the two candidates: last three elements or first two (negatives) times the last.
Track top-3 largest and bottom-2 smallest values in a single pass without sorting.
Two negatives can produce a large positive when multiplied by the largest positive.
Compare max(abc, xyz) where a,b,c are top-3 and x,y are bottom-2 and z is top-1.
Optimal Approach
Sort the array. The maximum product is either the product of the three largest numbers or the product of the two smallest (both negative) numbers and the largest number. Return the larger of these two values. This runs in O(n log n) time due to sorting, or O(n) with a single-pass tracking approach.
Sort, then compare two products
What Trips People Up in Real Interviews
Clarify integer range — products can overflow 32-bit int, use 64-bit or long.
Negative numbers matter: two negatives make a positive, so the product of two smallest and the largest may win.
The naive O(n log n) sort approach is fine for interviews but mention the O(n) single-pass alternative.
Test with [1, 2, 3], [-10, -10, 1, 2, 3], and [-5, -4, -3, -2, -1].
Edge case: exactly three elements — just return their product.
Solution Code
O(n log n) timedef maximumProduct(nums: list[int]) -> int:
nums.sort()
return max(nums[-1] * nums[-2] * nums[-3], nums[0] * nums[1] * nums[-1])Frequently Asked Questions
What is the Maximum Product of Three Numbers problem?
Given an integer array, find three numbers whose product is maximum and return that product. You may assume that the input always has at least three integers.
How do you solve Maximum Product of Three Numbers?
The optimal approach runs in O(n log n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Maximum Product of Three Numbers?
Maximum Product of Three Numbers is asked at Salesforce. It is a easy difficulty problem.
What are common mistakes on Maximum Product of Three Numbers?
- Clarify integer range — products can overflow 32-bit int, use 64-bit or long.
- Negative numbers matter: two negatives make a positive, so the product of two smallest and the largest may win.
- The naive O(n log n) sort approach is fine for interviews but mention the O(n) single-pass alternative.
- Test with [1, 2, 3], [-10, -10, 1, 2, 3], and [-5, -4, -3, -2, -1].
- Edge case: exactly three elements — just return their product.