Data Structures & AlgorithmsModerateTCS NQT · sorting
Problem: What is the number of comparisons needed by heapsort in the worst case for n elements?
- A. O(n log n)
- B. O(n)
- C. O(n^2)
- D. O(log n)
Correct answer: A. O(n log n)
Heapify is O(n) and each of the n extractions costs O(log n), so the total is O(n log n).
More Coding Interview Problems questions
- Problem: Which algorithm finds the k-th smallest element in expected linear time?
- Problem: Which property does the KMP failure function of a string satisfy?
- Problem: Which is the time complexity of deciding whether an undirected graph has an Euler path?
- Problem: How many pairs (i, j) with 1 <= i < j <= 20 satisfy i j ≡ 1 (mod 7)? Run the code. python print(sum(1 for i in range(1, 21) for j…
- Problem: What is the smallest n such that n! exceeds 10^10? Run the code. python import math n = 1 while math.factorial(n) <= 10 10: n +=…
- Problem: What is the sum of the lengths of all subarrays of an array of length 6? Run the code. python n = 6 print(sum(i (n - i + 1) for i…