ProgrammingHardTCS NQT · Wipro pattern
Which operation is O(1) amortised in CPython and removes the first item, while list.pop(0) is O(n)?
- A. list.pop()
- B. deque.popleft()
- C. list.insert(0, x)
- D. x in list
Correct answer: B. deque.popleft()
deque stores items in blocks at both ends, so removing from the left is constant time. list.pop(0) shifts every remaining item, which costs O(n).
More Python Programming questions
- What is printed? python print(sorted('bca', reverse=True)[0], 'bca'.index('c'))
- What is printed? python from collections import defaultdict d = defaultdict(int) d['x'] += 2 d['y'] print(dict(d))
- What is printed? python print(list(map(pow, [2, 3], [3, 2])))
- What is printed? python print(min(['10', '9', '2']), min([10, 9, 2]))
- What is printed? python s = {1, 2} t = s t |= {3} print(s, s is t)
- What is printed? python print(int(3.99), int(-3.99), round(3.5), round(4.5))