Good morning! Here's our prompt for today.
Given an unsorted array of integers, can you write a method maxProductOfThree(unsorted: array)
to find the largest product from three of the numbers? For example, given the following array:
[-1, 9, 22, 3, -15, -7]
The largest product of three numbers is 2310
. This results from -15 * -7 * 22
.

Constraints
- Length of the array <=
100000
- The array will contain values between
-1000
and1000
- The final answer would always fit in the integer range
- Expected time complexity :
O(n * log n)
- Expected space complexity :
O(1)
Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxx
44
var assert = require('assert');
function maxProductOfThree(unsorted) {
return unsorted;
}
try {
assert.deepEqual(maxProductOfThree([0, 1, 2, 3]), 6);
console.log('PASSED: ' + 'maxProductOfThree[0, 1, 2, 3] should equal 6');
} catch (err) {
console.log(err);
}
try {
assert.deepEqual(maxProductOfThree([-12, -7, -1, 11, 17]), 1428);
console.log(
'PASSED: ' + 'maxProductOfThree([-12, -7, -1, 11, 17]) should equal 1428'
);
} catch (err) {
console.log(err);
}
try {
assert.deepEqual(maxProductOfThree([0, 4, -9, 19, 7, -5]), 855);
console.log(
'PASSED: ' + 'maxProductOfThree([0, 4, -9, 19, 7, -5]) should equal 855'
);
} catch (err) {
console.log(err);
}
try {
assert.equal(maxProductOfThree([-1, 9, 22, 3, -15, -7]), 2310);
console.log(
'PASSED: ' + 'maxProductOfThree([-1, 9, 22, 3, -15, -7] should return 2310'
);
} catch (err) {
console.log(err);
}
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment
Tired of reading? Watch this video explanation!
To change the speed of the video or see it in full screen, click the icons to the right of the progress bar.

Here's our guided, illustrated walk-through.
How do I use this guide?
Access all course materials today
The rest of this tutorial's contents are only available for premium members. Please explore your options at the link below.