ProgrammingHardCodeforces-style · placement coding round
A Python program must answer 10^6 membership queries (x in container) against a container with 10^6 items. Which statement is correct?
- A. The list version does about 10^12 comparisons and the set version about 10^6 hash lookups
- B. Both versions do about 10^6 operations because list membership uses binary search
- C. The set version does about 10^12 hash lookups and the list version about 10^6
- D. Both versions are O(log n) per query
Correct answer: A. The list version does about 10^12 comparisons and the set version about 10^6 hash lookups
A list membership test scans linearly, which is O(n) per query, so 10^6 queries cost about 10^12 comparisons. A set uses hashing with O(1) average lookup, so the same queries take about 10^6 operations.
More Python Programming questions
- What is printed? python def ways(coins, amt): dp = [0] (amt + 1) dp[0] = 1 for c in coins: for v in range(c, amt + 1): dp[v] += dp[v - c]…
- What is printed? python def lcs(a, b): dp = [[0] (len(b) + 1) for in range(len(a) + 1)] for i in range(1, len(a) + 1): for j in range(1…
- What is printed? python items = [(60, 10), (100, 20), (120, 30)] cap = 50 best = 0 w = 0 for v, wt in sorted(items, key=lambda x: x[0] /…
- What is printed? python def mincapacity(w, days): lo, hi = max(w), sum(w) while lo < hi: mid = (lo + hi) // 2 need, cur = 1, 0 for x in w…
- What is printed? python from functools import lrucache @lrucache(maxsize=2) def sq(x): return x x for v in [1, 2, 1, 3, 1]: sq(v)…
- What is printed? python def nge(a): res = [-1] len(a) st = [] for i, x in enumerate(a): while st and a[st[-1]] < x: res[st.pop()] = x…