Data Structures & AlgorithmsModerateTCS NQT · Accenture

Problem: A string of length 6 is formed using letters only from {a, b}, containing no substring "aa" or "bb". How many such strings are there?

  1. A. 3
  2. B. 2
  3. C. 1
  4. D. 4

Correct answer: B. 2

Such strings must alternate, so the first letter determines the whole string, leaving 2 strings. Counting strings avoiding only one of the two patterns gives a larger count.

More Coding Interview Problems questions

Exams that ask this topic