Data Structures & AlgorithmsModerateCodeforces Div2 B · string round

Problem: What is the lexicographically smallest rotation of 'bcabca'?

Problem: What is the lexicographically smallest rotation of 'bcabca'?
s = 'bcabca'
print(min(s[i:] + s[:i] for i in range(len(s))))
What is printed?
  1. A. bcabca
  2. B. cabcab
  3. C. abcabc
  4. D. abcab

Correct answer: C. abcabc

The rotation starting at the first 'a' is abcabc, and it is the smallest of all six rotations. Printing the original string is the trap for those who forget to rotate.

More Coding Interview Problems questions

Exams that ask this topic