Data Structures & AlgorithmsModerateCodeforces Div2 B · counting

Problem: How many binary strings of length 4 avoid the substring '11'? Run the code.

Problem: How many binary strings of length 4 avoid the substring '11'? Run the code.
print(sum(1 for t in range(16) if '11' not in format(t, '04b')))
What is printed?
  1. A. 5
  2. B. 6
  3. C. 9
  4. D. 8

Correct answer: D. 8

The count follows Fibonacci: f(n) = f(n-1) + f(n-2) with f(1)=2, f(2)=3, f(3)=5, f(4)=8.

More Coding Interview Problems questions

Exams that ask this topic