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?

  1. A. Specialized algorithms need O(n^3) time, worse than naive sorting
  2. B. Naive sorting is already O(n log n), the same as specialized algorithms
  3. 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
  4. 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

Exams that ask this topic