ProgrammingHardCodeforces-style · placement coding round

In CPython, which operation on a collections.deque is O(n) in the worst case?

  1. A. d[i] for an index in the middle of the deque
  2. B. popleft()
  3. C. appendleft(x)
  4. D. len(d)

Correct answer: A. d[i] for an index in the middle of the deque

Access at the two ends is O(1), but indexing into the middle walks through internal blocks, so it is O(n) in the worst case. Use a list or a different structure when you need fast middle access.

More Python Programming questions

Exams that ask this topic