ProgrammingModerateProduct-company MCQ pattern
Which statement about sorting 10^6 floats with sorted() in CPython is correct?
- A. Timsort is O(n log n) in the worst case and close to O(n) when the input is already sorted
- B. It always runs in O(n) because each float comparison takes constant time
- C. It is quicksort and so is O(n squared) in the worst case
- D. It is O(n log n) only when the input has no duplicates
Correct answer: A. Timsort is O(n log n) in the worst case and close to O(n) when the input is already sorted
sorted() uses Timsort, which detects existing runs and merges them, so already-sorted input takes about n comparisons. The worst case is still n log n comparisons.
More Python Programming questions
- Which operation is O(1) amortised in CPython?
- What is printed? python from functools import lrucache @lrucache(maxsize=None) def f(a, b=0): return a + b f(1, 2) f(1, b=2) f(1)…
- What is printed? python from collections import OrderedDict od = OrderedDict() for k in ['a', 'b', 'c']: od[k] = ord(k) od.movetoend('a')…
- What is printed? python b = [3, 1, 2] a = b.sort() c = sorted(b, reverse=True) print(a, b, c)
- What is printed? python def kadane(a): best = cur = a[0] start = bestl = bestr = 0 for i in range(1, len(a)): if cur < 0: cur = a[i] start…
- What is printed? python def threesum(a): a = sorted(a) out = [] for i in range(len(a) - 2): if i and a[i] == a[i - 1]: continue l, r = i +…