Data Structures & AlgorithmsModerateTCS NQT Digital · Infosys

Problem: Count the sequences of length 5 over {0, 1, 2} with no two adjacent equal digits and whose first digit is 0.

  1. A. 48
  2. B. 32
  3. C. 16
  4. D. 8

Correct answer: C. 16

The first digit is fixed as 0 and each later digit has 2 choices (anything but its predecessor), so 2⁴ = 16. Not fixing the first digit gives 3 · 2⁴ = 48, giving all five positions 2 choices gives 32, and 2³ = 8 is length 4.

More Coding Interview Problems questions

Exams that ask this topic