ProgrammingHardInfosys · Cognizant

Using Booth-style thinking, the lexicographically smallest rotation of the string "banana" is wanted. The procedure below tries every rotation and prints the smallest. What is printed?

Using Booth-style thinking, the lexicographically smallest rotation of the string "banana" is wanted. The procedure below tries every rotation and prints the smallest. What is printed?
for k = 0 to n - 1
    rotation = s[k..n-1] + s[0..k-1]
end for
Print the minimum rotation
  1. A. nanaba
  2. B. abanan
  3. C. anaban
  4. D. banana

Correct answer: B. abanan

The rotations are banana, ananab, nanaba, anaban, nabana and abanan, and the smallest is abanan. The largest rotation is the mirror mistake, the second smallest is anaban, and printing the original word ignores rotation.

More Pseudocode and Code Tracing questions

Exams that ask this topic