ProgrammingModerateTCS NQT · Accenture
For a Python dict d with n entries, which operation costs O(n) rather than O(1) on average?
- A. list(d.keys())
- B. k in d
- C. d.get(k)
- D. d[k] = v
Correct answer: A. list(d.keys())
Membership, lookup and assignment use hashing and cost O(1) on average. Building a list of the keys must copy all n keys, which costs O(n).
More Python Programming questions
- With n = 2 x 10^5 and a 1-second limit in CPython, which algorithm can be rejected because it is too slow?
- A solution loops over 2 x 10^5 queries, and each query runs if x in lst on a list of 2 x 10^5 items. Which change keeps the same answers…
- What is printed? python def tri(t): dp = t[-1][:] for row in range(len(t) - 2, -1, -1): for j in range(len(t[row])): dp[j] = t[row][j] +…
- What is printed? python def lps(s): n = len(s) dp = [[0] n for in range(n)] for i in range(n): dp[i][i] = 1 for L in range(2, n + 1): for…
- What is printed? python fs = [lambda: i for i in range(3)] gs = [lambda i=i: i for i in range(3)] print([f() for f in fs], [g() for g in…
- What is printed? python def isprime(n): if n < 2: return False i = 2 while i i <= n: if n % i == 0: break i += 1 else: return True return…