Data Structures & AlgorithmsHardProduct placement · Amazon/Microsoft
A Python 2-D list is created two ways and one cell is set. What is printed?
A Python 2-D list is created two ways and one cell is set. What is printed?
a = [[0] * 3] * 3 b = [[0] * 3 for _ in range(3)] a[0][0] = 5 b[0][0] = 5 print(sum(map(sum, a)), sum(map(sum, b)))
- A. 15 4
- B. 15 6
- C. 5 5
- D. 15 5
Correct answer: D. 15 5
[[0] * 3] * 3 repeats a reference to ONE inner list, so a single assignment changes all three rows; the comprehension builds independent rows. Output: 15 5.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- A circular queue in an array of size 7 keeps one slot empty to tell full from empty (front = rear means empty; (rear + 1) mod 7 = front…
- Count distinct values in every window of size k with a frequency map. What is printed? python from collections import Counter a, k = [4…
- What is the minimum number of stacks needed to evaluate a fully parenthesised infix expression directly (without converting it first)…
- Merge sort and quick sort are compared. Which statement is correct?
- Bars of width 1 have heights [7, 3, 3, 6, 1, 3, 7, 1, 1]. How many units of rain water are trapped between them (two-pointer method with…
- Bubble sort (ascending; each pass compares adjacent pairs left to right and swaps if out of order) is applied to [49, 52, 21, 37, 35, 19…