# Coding Interview Question – Maximum Product of Three Numbers

Here is the Most Frequently Asked coding interview question by Big tech companies.

## Question: Given an integer array `nums`, find three numbers whose product is maximum and return the maximum product.

Sample data:

Input:

nums = [1,2,3,4]
Output: 24

Input:

nums = [12,2,-4,5,6,0,-9,4]
Output: 432

Let’s talk about the solution. First we need to create different products.

In first sample Input, we can have 4 different combinantions.

Input : [1,2,3,4]

With first element 1, we can have,

1 x 2 x 3, 1 x 2 x 4, 1 x 3 x 4, 2 x 3 x 4

So Now if we compare

1 x 2 x 3 = 6,

1 x 2 x 4 = 8,

1 x 3 x 4 = 12,

2 x 3 x 4 = 24

We can easily say 24 is the answer. So we can use brute force solution to add this logic in action. We can use for loops and create all these combination and compare them and return the maximum.

``````const prodTest = (arr) => {

let maxp = 0;

for (let i = 0; i < arr.length; i++) {

for (let j = i+1; j < arr.length; j++) {

for (let k = j+1; k < arr.length; k++) {
let product = arr[i] * arr[j] * arr[k];
if (product > maxp) {
maxp = product;
}

}
}
}
return maxp;
}
console.log(prodTest([1,2,3]))``````

As we are using three for loops, time complexity would be O(N^3) which is not efficient programming. So we have to figure out the way where we can create those combinations by using just one for loop to achive O(N) Time complexity.

So to do that, we will create the highest , lowest point and then highest product of 2, lowest of product of 2 and current Max product of 3. And we can keep checking Max from either product3, current X highest product2, current X lowest product2.

With mathematical calculation we can easily check which combination will be maximum without checking all combinations.

Now let’s add that logic into action for working solution.

``````const productOfThree = (arr) => {
if (arrayOfInts.length < 3) {
throw new Error('Less than 3 items!');
}

// We're going to start at the 3rd item (at index 2)
// So pre-populate highests and lowests based on the first 2 items
// We could also start these as null and check below if they're set
// but this is arguably cleaner

let highest = Math.max(arr, arr);
let lowest = Math.min(arr, arr);
let highest2 = arr * arr;
let lowest2 = arr * arr;

// Except this one--we pre-populate it for the first *3* items
// This means in our first pass it'll check against itself, which is fine

let product3 = arr * arr * arr;

// Walk through items, starting at index 2

for (let i = 2; i < arr.length; i++) {
const current = arr[i];
// Do we have a new highest product of 3?
// It's either the current highest
// or the current times the highest product of two
// or the current times the lowest product of two

product3 = Math.max(product3, highest2 * current, lowest2 * current);

// Do we have a new highest product of two?

highest2 = Math.max(highest2, highest * current, lowest * current);

// Do we have a new lowest product of two?

lowest2 = Math.min(lowest2, highest * current, lowest * current);
// Do we have a new highest?

highest =  Math.max(highest, arr[i]);
// Do we have a new lowest?

lowest = Math.min(lowest, arr[i]);
}
return product3;
}
console.log(productOfThree([12,2,-4,5,6,0,-9,4]));``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.8083906,"math_prob":0.9935709,"size":3190,"snap":"2021-43-2021-49","text_gpt3_token_len":926,"char_repetition_ratio":0.15505336,"word_repetition_ratio":0.067226894,"special_character_ratio":0.32570532,"punctuation_ratio":0.16432585,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9961329,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-23T11:01:24Z\",\"WARC-Record-ID\":\"<urn:uuid:4bca6675-33d3-41ca-8044-6fa8d949f17a>\",\"Content-Length\":\"28407\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:3fe63004-6d2d-476b-9d3f-85b80e87e309>\",\"WARC-Concurrent-To\":\"<urn:uuid:bfb5e8e6-24b3-42db-818d-55e1a676d2da>\",\"WARC-IP-Address\":\"208.109.75.245\",\"WARC-Target-URI\":\"http://dglobaltech.com/maximum-product-of-three/\",\"WARC-Payload-Digest\":\"sha1:M3L5DHFBI7AC7V7JPHIQ73CY7DKW4MPQ\",\"WARC-Block-Digest\":\"sha1:TBLADGNRITU4VVSQB46HYLBO4VP6HAS3\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323585671.36_warc_CC-MAIN-20211023095849-20211023125849-00092.warc.gz\"}"}