Data Structures & AlgorithmsHardICPC regional · string round

Problem: The suffix array of a string is its suffix start indices sorted by the suffix text. What is the value at position 2 (0-based) for s = 'banana'? Run the code.

Problem: The suffix array of a string is its suffix start indices sorted by the suffix text. What is the value at position 2 (0-based) for s = 'banana'? Run the code.
s = 'banana'
sa = sorted(range(len(s)), key=lambda i: s[i:])
print(sa[2])
What is printed?
  1. A. 1
  2. B. 3
  3. C. 0
  4. D. 4

Correct answer: A. 1

The sorted suffixes are a, ana, anana, banana, na, nana, so position 2 holds the suffix 'anana' starting at index 1. Sorting by start index instead of suffix text gives the trap 0.

More Coding Interview Problems questions

Exams that ask this topic