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?
- A. O(n)
- B. O(n^2)
- C. O(n^3)
- 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
- A solution does if x not in seenlist for each of n = 2 10^5 values, where seenlist grows to n items. Which change gives the best speedup…
- What is printed? python import heapq print(heapq.nsmallest(0, [5, 1]), max([], default=-1), min([3, 1, 2], key=lambda x: -x))
- What is printed? python print(pow(2, -1, 10 9 + 7))
- What is printed? python import math n = 10 30 - 1 print(math.isqrt(n), int(n 0.5))
- What is printed? python print(2 53 + 1 == float(2 53 + 1))
- What is printed? python import io s = io.StringIO("abcn") print(repr(s.readline()), repr(s.readline()))