Data Structures & AlgorithmsModerateTCS NQT Digital · Accenture

Problem: How many binary strings of length 5 contain no three consecutive 1s?

  1. A. 13
  2. B. 8
  3. C. 29
  4. D. 24

Correct answer: D. 24

a(n) = a(n−1) + a(n−2) + a(n−3) with 1, 2, 4 gives 7, 13, 24 for n = 3, 4, 5. Banning '11' gives 13, 8 counts the strings that do contain 111, and banning only '1111' gives 29.

More Coding Interview Problems questions

Exams that ask this topic