Data Structures & AlgorithmsEasyWipro · Accenture pattern
What is printed? (Python list aliasing)
What is printed? (Python list aliasing)
a = [1, 2, 3] b = a c = a[:] b.append(4) c.append(5) print(a, len(b), len(c))
- A. [1, 2, 3, 4, 5] 5 4
- B. [1, 2, 3, 4] 4 4
- C. [1, 2, 3] 4 4
- D. [1, 2, 3, 4] 4 3
Correct answer: B. [1, 2, 3, 4] 4 4
b = a makes b another name for the same list, so appending 4 through b changes a. a[:] is a shallow copy, so appending 5 to c leaves a alone. Both b and c end with 4 elements.
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
- What is printed? (frequency of the most common character, ties to the earliest) python s = "programming" best = max(s, key=s.count)…
- Sliding window: what is printed? (maximum sum of any 3 consecutive elements) c int a[] = {4, -1, 6, -2, 3, 5, -4, 2}; int n = 8, k = 3, s…
- What is printed? (two-pointer: remove all occurrences of a value in place) cpp vector<int> a = {3, 2, 2, 3, 4, 3, 5}; int w = 0; for (int…
- What is printed? (stack: check if a sequence of pops is possible after pushing 1..n in order) python def valid(pushed, popped): st, j =…
- What is printed? (next greater element to the right, −1 if none) python a = [4, 5, 2, 25, 7, 8] res, st = [-1] len(a), [] for i in…
- Hash map counting: what is printed? (first element that repeats, scanning left to right) python def firstrepeat(a): seen = set() for x in…