Data Structures & AlgorithmsHardInfosys · Accenture pattern

For [2, 3, 0, 4], out[i] is the product of all elements except a[i], computed with prefix and suffix products (no division). What is out?

  1. A. [0, 0, 0, 0]
  2. B. [0, 0, 24, 0]
  3. C. [12, 8, 24, 6]
  4. D. [0, 0, 24, 6]

Correct answer: B. [0, 0, 24, 0]

Only the position of the single zero gets a non-zero product: 2·3·4 = 24. Every other position includes the 0 → 0. The division trick (total / a[i]) fails here because the total is 0 and 0/0 is undefined; [12, 8, 24, 6] is what you get by ignoring the zero.

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

Exams that ask this topic