Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice

Problem: How many subsets of {1, 2, ..., 10} contain no two consecutive integers?

Problem: How many subsets of {1, 2, ..., 10} contain no two consecutive integers?
from itertools import combinations
print(sum(1 for m in range(1 << 10) if all(not ((m >> i) & 1 and (m >> (i + 1)) & 1) for i in range(9))))
What is printed?
  1. A. 89
  2. B. 233
  3. C. 144
  4. D. 123

Correct answer: C. 144

Subsets of a path of 10 elements with no two adjacent satisfy f(n) = f(n − 1) + f(n − 2) with f(1) = 2, f(2) = 3, so f(10) = F(12) = 144. Shifting the Fibonacci index by one gives 89 or 233, and treating the elements as a circle (also forbidding elements 0 and 9 together) gives the Lucas number 123.

More Coding Interview Problems questions

Exams that ask this topic