Maximum Product of Three Numbers

EasyMathmath

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given an integer array nums with at least three elements, choose three elements at distinct indices so that their product is as large as possible, and return that product. Values can be negative or zero, so the best triple is not always the three largest numbers: two large-magnitude negatives multiplied by the largest positive can win. Aim for O(n) time (a sort-based O(n log n) solution is also acceptable).

Examples

Input: nums = [1,2,3]
Output: 6
The only triple is 1 × 2 × 3.
Input: nums = [-10,-10,1,3,2]
Output: 300
(-10) × (-10) × 3 = 300 beats 3 × 2 × 1 = 6.

Constraints

  • 3 <= nums.length <= 10^4
  • -1000 <= nums[i] <= 1000
  • The answer fits in a 32-bit signed integer.

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More math problems