Data Structures & AlgorithmsModerateProduct Company SDE · simulation round

Problem: A row of 5 cells starts as [0, 0, 1, 0, 0]. Each step, every cell becomes the XOR of its two neighbours (cells outside the row count as 0). After 2 steps, what is the sum of the cells? Run the simulation.

Problem: A row of 5 cells starts as [0, 0, 1, 0, 0]. Each step, every cell becomes the XOR of its two neighbours (cells outside the row count as 0). After 2 steps, what is the sum of the cells? Run the simulation.
s = [0, 0, 1, 0, 0]
for _ in range(2):
    s = [(s[i - 1] if i > 0 else 0) ^ (s[i + 1] if i + 1 < len(s) else 0) for i in range(len(s))]
print(sum(s))
What is printed?
  1. A. 1
  2. B. 3
  3. C. 2
  4. D. 0

Correct answer: C. 2

After step 1 the row is [0,1,0,1,0] and after step 2 it is [1,0,0,0,1], which sums to 2. Forgetting the boundary zeros changes the result.

More Coding Interview Problems questions

Exams that ask this topic