Embercode
✕
Dynamic ProgrammingQuick quiz

Maximum product

6 questions. Covers Maximum product subarray in Python.

Questions 1 to 5 of 6

  1. 1. What does this program print? ``python def max_product(nums): max_end = min_end = best = nums[0] for x in nums[1:]: a, b, c = x, max_end * x, min_end * x max_end = max(a, b, c) min_end = min(a, b, c) best = max(best, max_end) return best print(max_product([2, -3, 4, -5])) ``
  2. 2. What does this program print? ``python def max_product(nums): max_end = min_end = best = nums[0] for x in nums[1:]: a, b, c = x, max_end * x, min_end * x max_end = max(a, b, c) min_end = min(a, b, c) best = max(best, max_end) return best print(max_product([-3, 0, -2])) ``
  3. 3. Why does the method keep min_end, the smallest product that ends at each position?
  4. 4. What does max_product return for the list [-5], and why?
  5. 5. Setting best to 0 at the start of max_product gives the right answer on every list.

5 question(s) left on this page.