Data Structures & AlgorithmsVery hardGoogle · Flipkart interview

Problem: Count the ways to tile a 2 × n board with 2 × 1 dominoes and L-shaped trominoes (both rotatable).

Problem: Count the ways to tile a 2 × n board with 2 × 1 dominoes and L-shaped trominoes (both rotatable).
def tilings(n):
    dp = [1, 1, 2] + [0] * n
    for i in range(3, n + 1):
        dp[i] = 2 * dp[i-1] + dp[i-3]
    return dp[n]
print(tilings(5))
What is printed?
  1. A. 8
  2. B. 21
  3. C. 24
  4. D. 29

Correct answer: C. 24

Sequence: 1, 1, 2, 5, 11, 24, using dp[n] = 2·dp[n−1] + dp[n−3], which comes from folding the tromino-pair cases into the recurrence. With dominoes only the count would be Fibonacci (8 for n = 5).

More Coding Interview Problems questions

Exams that ask this topic