ProgrammingModerateProduct-company MCQ pattern

Which operation is O(1) amortised in CPython?

  1. A. list.insert(0, x)
  2. B. list.append(x)
  3. C. str.replace on a long string
  4. D. list.pop(0)

Correct answer: B. list.append(x)

list.append adds at the end and reallocates in bulk, so the cost averages out to O(1). insert and pop at index 0 shift every element, which costs O(n).

More Python Programming questions

Exams that ask this topic