Data Structures & AlgorithmsVery hardTCS Digital · Product companies

What is the maximum product of a non-empty contiguous subarray of [2, -5, -2, -4, 3]?

  1. A. 20
  2. B. 10
  3. C. 8
  4. D. 24

Correct answer: D. 24

Track both the running maximum and minimum product, since a negative can flip them. The best block is [-2, -4, 3] = 24; the whole array is -240, [2, -5, -2] gives only 20 and [-5, -2] only 10.

More Coding Interview Problems questions

Exams that ask this topic