EasyArrayMathSortingUpdated Sep 2026

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

nums (sorted)-10-1012301234Blue = two smallest, green = three largest. The answermust use one of these two groups.three largest: 1 * 2 * 3 = 6two smallest * largest: -10 * -10 * 3 = 300answer = max(6, 300) = 300
In [-10,-10,1,2,3] the two smallest values times the largest (300) beat the three largest values (6).

Constraints

  • ●3 <= nums.length <= 10^4
  • ●-1000 <= nums[i] <= 1000

Examples

Example 1
Inputnums = [1,2,3]
Output6
WhyThere is only one way to pick three numbers, so the product is 1 * 2 * 3 = 6.
Example 2
Inputnums = [1,2,3,4]
Output24
WhyAll numbers are positive, so taking the three biggest gives the best result: 2 * 3 * 4 = 24.
Example 3
Inputnums = [-1,-2,-3]
Output-6
WhyThe only possible product is (-1) * (-2) * (-3) = -6. With three negatives the result must be negative.

Asked At

1 companies
CompanyDifficulty
SalesforceEasyView all Salesforce questions →

How to Think About It

1

Brute force: check every triplet with three nested loops — O(n^3).

2

Sort the array first and consider the two candidates: last three elements or first two (negatives) times the last.

3

Track top-3 largest and bottom-2 smallest values in a single pass without sorting.

4

Two negatives can produce a large positive when multiplied by the largest positive.

5

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

nums (sorted)two smallestthree largest-10-1012301234Step 1 — sort the array so the extremes are easy to reach.nums (sorted)-10-1012301234Step 2 — candidate A uses indices 0, 1 and n-1: -10 * -10 * 3 = 300.nums (sorted)-10-1012301234Step 3 — candidate B uses the last three indices: 1 * 2 * 3 = 6.answer = max(nums[0]*nums[1]*nums[-1], nums[-1]*nums[-2]*nums[-3300 > 6, so return 300
After sorting, the two candidate groups sit at the two ends of the array, so one comparison finishes the job.
ComplexityTime O(n log n)Space O(1)Sorting the array dominates the cost; the two candidate products are then computed in constant time.

What Trips People Up in Real Interviews

1

Clarify integer range — products can overflow 32-bit int, use 64-bit or long.

2

Negative numbers matter: two negatives make a positive, so the product of two smallest and the largest may win.

3

The naive O(n log n) sort approach is fine for interviews but mention the O(n) single-pass alternative.

4

Test with [1, 2, 3], [-10, -10, 1, 2, 3], and [-5, -4, -3, -2, -1].

5

Edge case: exactly three elements — just return their product.

Solution Code

O(n log n) time
def maximumProduct(nums: list[int]) -> int:
    nums.sort()
    return max(nums[-1] * nums[-2] * nums[-3], nums[0] * nums[1] * nums[-1])

Pro at DSA?

Test your skills with a real FAANG-style mock interview.

Start a Mock Interview →

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.