ProgrammingHardCodeforces-style · placement coding round
A program sorts the whole list again for each of 10^5 queries on a list of 10^5 items. Which statement about running it in CPython is correct?
- A. Yes, sorting is O(n) in Python
- B. Yes, sorted results are cached per list
- C. No, about 10^5 sorts of 10^5 items is far beyond one second
- D. Only if the list is already sorted
Correct answer: C. No, about 10^5 sorts of 10^5 items is far beyond one second
Each sort costs about n log n, which is roughly 1.7 x 10^6 steps, and repeating it 10^5 times gives about 10^11 steps. The fix is to sort once and answer the queries from that order.
More Python Programming questions
- What is printed? python from collections import Counter def mw(s, t): need = Counter(t) have = {} formed = 0 req = len(need) l = 0 best =…
- What is printed? python import bisect board = [] for name, score in [('a', 5), ('b', 9), ('c', 5)]: bisect.insort(board, (-score, name))…
- What is printed? python print(sum([1, 2], 0.5))
- What is printed? python import heapq def topo(n, edges): indeg = [0] n adj = [[] for in range(n)] for u, v in edges: adj[u].append(v)…
- Which operation is O(1) amortised in CPython and removes the first item, while list.pop(0) is O(n)?
- What is printed? python print(sorted('bca', reverse=True)[0], 'bca'.index('c'))