ProgrammingModerateAmazon OA · Flipkart coding round

What is printed?

What is printed?
def prod_except(a):
    n = len(a)
    out = [1] * n
    left = 1
    for i in range(n):
        out[i] = left
        left *= a[i]
    right = 1
    for i in range(n - 1, -1, -1):
        out[i] *= right
        right *= a[i]
    return out
print(prod_except([1, 2, 3, 4]), prod_except([0, 5, 2]))
  1. A. [24, 12, 8, 6] [10, 0, 0]
  2. B. [24, 12, 8, 6] [0, 0, 10]
  3. C. [24, 12, 8, 6] [10, 0, 1]
  4. D. [24, 12, 6, 8] [10, 0, 0]

Correct answer: A. [24, 12, 8, 6] [10, 0, 0]

A left-to-right pass stores the product of the earlier items, then a right-to-left pass multiplies in the product of the later items. A zero in the input forces zeros in the other positions.

More Python Programming questions

Exams that ask this topic