ProgrammingHardCodeforces-style · placement coding round
With n = 2 x 10^5 and a 1-second limit in CPython, which algorithm can be rejected because it is too slow?
- A. Sort the array once and use two pointers, about 3.5 x 10^6 steps
- B. A nested loop over all pairs, about 2 x 10^10 checks
- C. A dictionary counting pass, about 2 x 10^5 steps
- D. A binary search for each element, about 3.5 x 10^6 steps
Correct answer: B. A nested loop over all pairs, about 2 x 10^10 checks
CPython manages roughly 10^7 simple operations per second, so n^2 pair checks with n = 2 x 10^5 is far too slow. The other three are O(n) or O(n log n).
More Python Programming questions
- 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…
- For a Python list a, which operation takes time proportional to j - i?