ProgrammingVery hardAmazon OA · Flipkart coding round

What is printed?

What is printed?
def max_prod(a):
    best = cur_max = cur_min = a[0]
    for x in a[1:]:
        cands = (x, cur_max * x, cur_min * x)
        cur_max, cur_min = max(cands), min(cands)
        best = max(best, cur_max)
    return best
print(max_prod([2, 3, -2, 4]), max_prod([-2, 3, -4]))
  1. A. 6 6
  2. B. 6 24
  3. C. 4 24
  4. D. 6 -4

Correct answer: B. 6 24

The code tracks both the running maximum and minimum, since a negative times a negative can become the new maximum. For [-2, 3, -4] the best product is (-2) x 3 x (-4) = 24.

More Python Programming questions

Exams that ask this topic