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)))
- A. 7
- B. 21
- C. 0
- 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
- Problem: What does this Python snippet print? print(-7 // 2, -7 % 3, 7 // -2)
- Problem: What does this snippet print? gen = (xx for x in range(4)) print(sum(gen), sum(gen))
- Problem: What does this snippet print? fs=[lambda: i for i in range(3)] print([f() for f in fs])
- Problem: What does this snippet print? s = "abcdefgh" print(s[::-3], s[5:1:-2])
- Problem: What is the value printed by this snippet? from collections import Counter print(Counter("abracadabra").mostcommon(2))
- Problem: Consider the list a = [2, 4, 6, 8]. The loop 'for x in a: if x % 2 == 0: a.remove(x)' is run. What is the list a afterwards?