ProgrammingHardCodeforces-style · placement coding round
In CPython, which operation on a collections.deque is O(n) in the worst case?
- A. d[i] for an index in the middle of the deque
- B. popleft()
- C. appendleft(x)
- D. len(d)
Correct answer: A. d[i] for an index in the middle of the deque
Access at the two ends is O(1), but indexing into the middle walks through internal blocks, so it is O(n) in the worst case. Use a list or a different structure when you need fast middle access.
More Python Programming questions
- What is printed? python def primes(n): s = [True] (n + 1) s[0:2] = [False, False] for i in range(2, int(n 0.5) + 1): if s[i]: s[i i::i] =…
- What is printed? python def merge(a, b): i = j = 0 out = [] while i < len(a) and j < len(b): if a[i] <= b[j]: out.append(a[i]) i += 1…
- What is printed? python from collections import deque def maxdist(g): R, C = len(g), len(g[0]) d = [[-1] C for in range(R)] q = deque()…
- What is printed? python from collections import Counter c = Counter('mississippi') print(c.mostcommon(2))
- What is printed? python def maxprod(a): best = curmax = curmin = a[0] for x in a[1:]: cands = (x, curmax x, curmin x) curmax, curmin =…
- What is printed? python def jumps(a): j = cur = far = 0 for i in range(len(a) - 1): far = max(far, i + a[i]) if i == cur: j += 1 cur = far…