ProgrammingModerateAmazon OA · Flipkart coding round

A program inserts n = 200000 items one at a time with `lst.insert(0, x)` and never removes them. Which is the time complexity of the whole loop?

  1. A. O(n)
  2. B. O(n^2)
  3. C. O(n^3)
  4. D. O(n log n)

Correct answer: B. O(n^2)

Each insert at the front shifts every existing element one slot right, which costs O(current size). Summing 1 + 2 + ... + n gives O(n^2). A deque with appendleft gives O(n) overall.

More Python Programming questions

Exams that ask this topic