ProgrammingHardTCS NQT · Wipro pattern

Which operation is O(1) amortised in CPython and removes the first item, while list.pop(0) is O(n)?

  1. A. list.pop()
  2. B. deque.popleft()
  3. C. list.insert(0, x)
  4. 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

Exams that ask this topic