ProgrammingModerateAmazon OA · Flipkart coding round

Why is `del a[0]` on a Python list O(n), while `a.pop()` is O(1)?

  1. A. The list stores its first item in a linked node that must be searched
  2. B. pop() copies the list, while del changes it in place without copying
  3. C. Deleting the first item shifts every remaining item one place left, while pop from the end changes nothing else
  4. D. Deleting the first item rebuilds the hash table, while pop from the end does not

Correct answer: C. Deleting the first item shifts every remaining item one place left, while pop from the end changes nothing else

A Python list is a contiguous array. Removing index 0 moves all the other items, which costs O(n). Removing the last item only shrinks the array.

More Python Programming questions

Exams that ask this topic