ProgrammingHardGATE CS · Product-company placement

For a Python list `a` with n elements, which operation is O(n) in the worst case (not amortised O(1))?

  1. A. a.append(x)
  2. B. a.insert(0, x)
  3. C. a.pop()
  4. D. a[-1] = x

Correct answer: B. a.insert(0, x)

A CPython list is a dynamic array: append and pop from the end are amortised O(1) and index assignment is O(1), but inserting at the front must shift all n existing elements.

More Python Programming questions

Exams that ask this topic