Data Structures & AlgorithmsHardProduct company OA · Amazon/Microsoft-style OA

How many strings of length 5 over the alphabet {a, b, c} have no two adjacent characters equal?

  1. A. 48
  2. B. 54
  3. C. 36
  4. D. 243

Correct answer: A. 48

Every valid string can be extended in exactly two ways at each step, because the next letter must differ from the previous one. So the total is 3 * 2^4 = 48; ignoring the restriction gives 3^5 = 243.

More Coding Interview Problems questions

Exams that ask this topic