Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice
Problem: Which statement about the time complexity of building a suffix array naively (sorting all n suffixes with standard string comparison) versus using an O(n log n) algorithm is correct?
- A. Specialized algorithms need O(n^3) time, worse than naive sorting
- B. Naive sorting is already O(n log n), the same as specialized algorithms
- C. Naive sorting costs O(n^2 log n) (each of the n log n comparisons can itself cost O(n)); specialized algorithms like prefix doubling sort suffixes in O(n log n) total
- D. Suffix arrays can never be built faster than O(n^2)
Correct answer: C. Naive sorting costs O(n^2 log n) (each of the n log n comparisons can itself cost O(n)); specialized algorithms like prefix doubling sort suffixes in O(n log n) total
Comparing two arbitrary suffixes directly can cost O(n) in the worst case, and a standard O(n log n)-comparison sort then costs O(n^2 log n) overall; prefix-doubling techniques instead sort suffixes by progressively longer prefixes using radix-sort-like merging, achieving O(n log n) total.
More Coding Interview Problems questions
- Problem: A delivery app logs the parcel weights of a day in an array. Return the indices of the two parcels whose weights add up exactly…
- Problem: Given an unsorted array of n ≤ 10⁵ integers and a target, report whether two different positions hold values summing to the…
- Problem: A stock tracker gets daily prices and must report the best profit from one buy followed by one later sell (0 if no profit is…
- Problem: Find the largest sum of a non-empty contiguous block of an array (values may be negative). python def best(a): cur = ans = a[0]…
- Problem: Rotate an array to the right by k steps in place, using O(1) extra space. Example: [1, 2, 3, 4, 5], k = 2 → [4, 5, 1, 2, 3]…
- Problem: Every number in a list of sensor IDs appears twice except one. Find the lonely ID in O(n) time and O(1) space. python def…