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)?
- A. 12
- B. 6
- C. 30
- 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
- Problem: Find the length of the longest substring of "aaabbbbcccd" that contains at most two distinct letters.
- Problem: How many numbers from 1 to 1000 have a digit sum divisible by 9?
- Problem: How many ways are there to write 20 as an ordered sum of parts from {2, 3, 5}?
- Problem: The sequence a0 = 0 and a(n) = a(n−1) + (n mod 3) for n ≥ 1 is defined. What is a(30)?
- Problem: A square matrix 4×4 has entry (i, j) equal to (i + 2j) mod 5. What is the sum of the entries on the main diagonal?
- Problem: The sum of the squares of the first 15 natural numbers, reduced modulo 7, is what value?