Data Structures & AlgorithmsHardTCS NQT Digital · Infosys
Problem: What does this print? x = [1, 2, 3] y = x y += [4] z = x[:] z.append(5) print(len(x), len(z))
- A. 3 5
- B. 4 4
- C. 4 5
- D. 3 4
Correct answer: C. 4 5
y and x are the same list object, so += changes x; z is a shallow copy taken after that change, so it has one more element than x after append. Assuming y is a copy leaves x with three elements.
More Coding Interview Problems questions
- Problem: What is printed by this snippet? d = {} d['b'] = 1 d['a'] = 2 d['b'] = 3 print(list(d.items()))
- Problem: What is the output? print(sorted({3,1,2} & {2,3,4}), 3 in {1,2,3}, len({1,1,2}))
- Problem: The number 1000000007 is used as a modulus in competitive programming. How many times does the decimal digit 7 appear in the…
- Problem: In a bit-trick loop, n = 0b101101 is processed by repeatedly computing n &= n − 1 until n becomes 0. How many iterations are…
- Problem: The XOR of the values 1 through 10 is XOR-ed with the XOR of the values 1 through 9. What is the resulting value?
- Problem: The list [5, 8, 1, 9, 3, 7, 2] is split into two halves (the first ⌈n/2⌉ elements and the rest). Each half is sorted, and the two…