790. Domino and Tromino Tiling
Asked at Goldman Sachs
Difficulty
Medium
Time
O(n)
Space
O(1)
Pattern
Dynamic Programming
Companies
1
Problem
You are tiling a board that is 2 rows tall and n columns wide. You have two kinds of tiles: a domino, which covers a 2 x 1 rectangle (two squares), and a tromino, which is an L-shape covering three squares. You may rotate either tile in any direction.
Count how many different ways you can cover the whole board with these tiles, with no gaps and no overlaps. Two tilings count as different if there is at least one pair of touching squares where one tiling covers both squares with a single tile and the other tiling does not.
Because the count can get huge, return it modulo 10^9 + 7.
Tiles and the board
Constraints
- ●
1 <= n <= 1000 - ●
Return the answer modulo 10^9 + 7
Examples
n = 35n = 11Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Goldman Sachs | Medium | View all Goldman Sachs questions → |
How to Think About It
With dominoes only, the answer is Fibonacci. Trominoes add tilings where the right edge is jagged, so you need to track more than "fully tiled up to column i".
Two-state DP: full[i] = ways to completely tile the first i columns; part[i] = ways to tile the first i columns plus one extra cell sticking out in column i + 1 (either top or bottom — symmetric, so count one side and double it).
Transitions: full[i] = full[i-1] + full[i-2] + 2 * part[i-2] and part[i] = part[i-1] + full[i-1].
These collapse into the well-known recurrence dp[i] = 2 * dp[i-1] + dp[i-3], with dp[0] = 1, dp[1] = 1, dp[2] = 2.
Check: dp[3] = 2 * 2 + 1 = 5, which matches the five tilings of a 2x3 board.
Optimal Approach
Step 1: Handle small n: dp[0] = 1, dp[1] = 1, dp[2] = 2.
Step 2: For i from 3 to n: dp[i] = (2 * dp[i-1] + dp[i-3]) % MOD.
Step 3: Return dp[n].
Time: O(n). Space: O(1) if you keep only the last three values.
DP recurrence dp[i] = 2*dp[i-1] + dp[i-3]
What Trips People Up in Real Interviews
Using Fibonacci. That ignores trominoes entirely.
Stating 2 * dp[i-1] + dp[i-3] with no derivation. Show the full/partial state transitions and then simplify — interviewers want to see where it comes from.
Forgetting the modulo or applying it only at the end, which overflows in fixed-width languages.
Wrong base cases. dp[0] = 1 (the empty tiling) matters for the recurrence.
Solution Code
O(n) timedef numTilings(n):
MOD = 10**9 + 7
dp = [1, 1, 2]
if n < 3:
return dp[n]
for i in range(3, n + 1):
dp.append((2 * dp[i - 1] + dp[i - 3]) % MOD)
return dp[n]Frequently Asked Questions
What is the Domino and Tromino Tiling problem?
Domino and Tromino Tiling asks how many ways you can tile a `2 x n` board using 2x1 dominoes and L-shaped trominoes, modulo `10^9 + 7`. It is a DP problem where the challenge is setting up states correctly — the L-shapes create partially filled columns that plain Fibonacci-style tiling does not have.
How do you solve Domino and Tromino Tiling?
The optimal approach runs in O(n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Domino and Tromino Tiling?
Domino and Tromino Tiling is asked at Goldman Sachs. It is a medium difficulty problem.
What are common mistakes on Domino and Tromino Tiling?
- Using Fibonacci. That ignores trominoes entirely.
- Stating `2 * dp[i-1] + dp[i-3]` with no derivation. Show the full/partial state transitions and then simplify — interviewers want to see where it comes from.
- Forgetting the modulo or applying it only at the end, which overflows in fixed-width languages.
- Wrong base cases. `dp[0] = 1` (the empty tiling) matters for the recurrence.