Community

Start a Thread


Notifications
Subscribe You’re not receiving notifications from this thread.

Max Product Of Three Numbers (Main Thread)

Here is the interview question prompt, presented for reference.

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 and 1000
  • The final answer would always fit in the integer range
  • Expected time complexity : O(n * log n)
  • Expected space complexity : O(1)

You can see the full challenge with visuals at this link.

Challenges • Asked over 4 years ago by Anonymous

Jake from AlgoDaily Commented on Aug 12, 2019:

This is the main discussion thread generated for Max Product Of Three Numbers.

Anonymous Commented on Aug 12, 2019:

Since you sort the algorithm you provided in O(nlogn) but if you just iterated once to find the first max product, then iterated again to find the second product, it would by O(n).

dlflann Commented on Apr 08, 2021:

This question and its solution are a bit misleading. In the question one of the constraints is an O(n) time complexity, but as pointed out by another comment, because you sort the best you can do is O(nlogn). The solution provided as an example does not meet the constraint laid out in the question.