Data Structures & AlgorithmsHardProduct Company SDE · Amazon OA

Problem: What does this Python snippet print? g = [[0]*3]*3 g[1][2] = 7 print(sum(map(sum, g)))

  1. A. 7
  2. B. 21
  3. C. 0
  4. D. 63

Correct answer: B. 21

[[0]*3]*3 repeats the SAME inner list three times, so g[1][2] = 7 shows up in every row and the total is 3 × 7 = 21. Independent rows (e.g. [[0]*3 for _ in range(3)]) would give 7, and 0 or 63 misread the aliasing in opposite directions.

More Coding Interview Problems questions

Exams that ask this topic