EASY
ArrayMathSorting
Updated Sep 2026

Maximum Product of Three Numbers

Asked at Salesforce

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.

Asked At

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.

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

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 is described in detail above, including step-by-step walkthroughs, complexity analysis, and solution code in Python. Scroll up to the "Optimal Approach" section.

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.