Data Structures & AlgorithmsHardTCS NQT Digital · Product Company SDE

Problem: How many sequences made of the numbers 1 and 2 sum to 8, where no two 2s are adjacent (order matters)?

  1. A. 34
  2. B. 7
  3. C. 5
  4. D. 19

Correct answer: D. 19

A DP on whether the last part was a 2 gives 19 sequences for sum 8. Ignoring the rule gives F(9) = 34, banning adjacent 1s instead gives 7, and 5 counts unordered multisets.

More Coding Interview Problems questions

Exams that ask this topic