Data Structures & AlgorithmsHardTCS NQT Digital · Infosys

Problem: Among the strings "abc", "bca", "cab", "acb", "bac", "cba", how many distinct pairs (x, y) with x ≠ y are rotations of each other (y is a cyclic shift of x)?

  1. A. 12
  2. B. 6
  3. C. 30
  4. D. 2

Correct answer: A. 12

Each rotation class {abc, bca, cab} and {acb, bac, cba} gives 3 · 2 = 6 ordered pairs, total 12. Unordered pairs give 6, 30 counts every ordered pair, and 2 counts the classes.

More Coding Interview Problems questions

Exams that ask this topic