Data Structures & AlgorithmsModerateTCS NQT · DP warm-up

Problem: How many ways tile a 1 x 4 strip with 1 x 1 squares and 1 x 2 dominoes? Run the code.

Problem: How many ways tile a 1 x 4 strip with 1 x 1 squares and 1 x 2 dominoes? Run the code.
f = [1, 1]
for _ in range(3):
    f.append(f[-1] + f[-2])
print(f[4])
What is printed?
  1. A. 4
  2. B. 6
  3. C. 8
  4. D. 5

Correct answer: D. 5

The tilings satisfy t(n) = t(n-1) + t(n-2) with t(0) = t(1) = 1, giving 1, 1, 2, 3, 5. The answer is t(4) = 5.

More Coding Interview Problems questions

Exams that ask this topic