Data Structures & AlgorithmsHardTCS Digital · Infosys SP
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][1] = 5 b[0][1] = 5 print(sum(map(sum, a)), sum(map(sum, b)))
- A. 15 5
- B. 5 5
- C. 15 6
- D. 15 7
Correct answer: A. 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
- An array of size 9 holds the numbers 1..9, except that one number is missing and one number appears twice: [6, 5, 1, 3, 4, 7, 2, 8, 2]…
- Overlapping versus non-overlapping substring counts. What is printed? python s, p = "aababaaabbba", "aba" over = sum(1 for i in…
- Bars of width 1 have heights [2, 3, 1, 5, 2, 1, 1, 6]. What is the area of the largest rectangle that fits under the histogram (computed…
- For an array of n elements, how many contiguous subarrays are there, and how many subsequences (including the empty one)?
- A monotonic deque reports the maximum of every window of size 3 in [19, 6, 7, 16, 10, 2, 12, 8, 17]. What is the SUM of all reported…
- Two pointers start at both ends of the sorted array [3, 4, 5, 6, 8, 17, 18, 23, 26, 27]. How many pairs (i < j) have a[i] + a[j] ≤ 40?…