Count the ways to tile a 2 x n board with dominoes and L-shaped trominoes
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
Overview: Count the ways to tile a 2 x n board with dominoes and L-shaped trominoes, any of which may be rotated, and return the count modulo 10^9 + 7. The problem tests whether you can derive a linear dynamic programming recurrence from how a tiling can end at the right edge.
Constraints
- 1 <= n <= 1000
- The answer is returned modulo 10^9 + 7, so it is an integer from 0 to 1000000006.
Examples
Input: (1,)
Expected Output: 1
Explanation: Minimum width from the source example: a single vertical domino is the only tiling.
Input: (2,)
Expected Output: 2
Explanation: Two vertical dominoes or two stacked horizontal dominoes; a tromino pair needs at least 3 columns.
Hints
- Work out the answers for widths 1 to 4 by hand (1, 2, 5 and 11) and look at what can occupy the rightmost columns of a finished tiling.
- A tromino leaves one empty cell inside its 2 x 2 square. Follow what has to fill that cell, and the cell after it, until the shape closes off again.
- Reduce modulo 10^9 + 7 as you go, since the exact count for n = 1000 has hundreds of digits.