ProgrammingHardTCS NQT · Accenture

What is printed?

What is printed?
def max3(a):
    a = sorted(a)
    return max(a[-1] * a[-2] * a[-3], a[0] * a[1] * a[-1])
print(max3([1, 2, 3, 4]), max3([-10, -10, 5, 2]))
  1. A. 24 500
  2. B. 12 -100
  3. C. 60 500
  4. D. 24 100

Correct answer: A. 24 500

The largest product of three is either the three biggest values or two very negative values times the biggest. For [-10, -10, 5, 2] the pair -10 x -10 gives 100, and times 5 that is 500.

More Python Programming questions

Exams that ask this topic