ProgrammingModerateTCS NQT · Accenture
For a Python list a, which operation takes time proportional to j - i?
- A. a[i]
- B. len(a)
- C. a.append(x)
- D. a[i:j]
Correct answer: D. a[i:j]
Slicing copies the selected references into a new list, so its cost grows with the slice length j - i. Indexing, len and append do not depend on the slice size.
More Python Programming questions
- What is printed? python from functools import lrucache @lrucache(maxsize=None) def f(n, last): if n == 0: return 1 total = f(n - 1, 0) if…
- What is printed? python a = [1, 2, 4] cnt = 0 for mask in range(1 << len(a)): s = sum(a[i] for i in range(len(a)) if mask >> i & 1) if s %…
- What is printed? python d = {'a': 1, 'b': 2, 'c': 3} d['a'] = 9 del d['b'] d['b'] = 5 print(list(d.items()))
- What is printed? python import bisect a = [(1, 'x'), (3, 'y'), (3, 'z'), (7, 'w')] i = bisect.bisectleft(a, 3, key=lambda p: p[0]) j =…
- Which statement about counting pairs i < j with a[i] + a[j] == t is FALSE?
- What is printed? python a = [] for i in range(20): a.append(i) print(len(a), a[-1], a.pop(), len(a))