Data Structures & AlgorithmsVery hardProduct company OA

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

  1. A. 8
  2. B. 24
  3. C. 6
  4. D. 2

Correct answer: B. 24

Track both the max and min product ending at each index (a negative times the min can become the max). After the 0 resets: −2, −4 → 8; × 3 → 24; × −1 → −24 (but the min −24 is not the answer). 24 from [−2, −4, 3]. Including the trailing −1 makes the product negative; across the 0 everything is 0.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic