Data Structures & AlgorithmsHardCodeforces Div2 C · combinatorics

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

  1. A. 8
  2. B. 21
  3. C. 13
  4. D. 12

Correct answer: C. 13

If f(n) counts valid strings of length n, then f(n) = f(n-1) + f(n-2) with f(1) = 2 and f(2) = 3, so f(5) = 13. Allowing any strings gives 32 and dropping the last-bit case gives the trap 8.

More Coding Interview Problems questions

Exams that ask this topic