ProgrammingModerateTCS CodeVita · Infosys HackWithInfy
Which of the following statements about Python's sort is correct for CP problems?
- A. Timsort is O(n^2) in the worst case, so it is unsafe for 2 * 10^5 items
- B. list.sort() and sorted() use Timsort, which is stable and runs in O(n log n) worst case
- C. sorted() sorts in place and returns None
- D. list.sort() is always faster than sorted() on the same list
Correct answer: B. list.sort() and sorted() use Timsort, which is stable and runs in O(n log n) worst case
Python's built-in sort is Timsort, which is stable and has O(n log n) worst-case time. sorted() returns a new list, and list.sort() sorts in place and returns None.
More Python Programming questions
- What is printed? python def solve(n): if n == 0: return [] return solve(n // 2) + [n % 2] print(solve(6), solve(0))
- What is printed? python print(divmod(7, -3), 7 % -3, -7 % 3, -7 // 3)
- What is printed? python x = 'aaa' y = list(x) y[0] = 'b' print(x, ''.join(y), x is y)
- What is printed? python grid = [[1, 2], [3, 4]] flat = [v for row in grid for v in row] print(flat, sum(flat) == sum(map(sum, grid)))
- What is printed? python def merge(a, b): out, i, j = [], 0, 0 while i < len(a) and j < len(b): if a[i] <= b[j]: out.append(a[i]); i += 1…
- Which change removes the RecursionError for a DFS over a path of 10^5 nodes, while keeping the same visiting order?