Domino and Tromino Tiling - Problem

Imagine you're a tile artist working on a 2×n board and you have two types of beautiful tiles at your disposal:

  • Domino tiles: 2×1 rectangular pieces that can be placed horizontally or vertically
  • Tromino tiles: L-shaped pieces made of 3 squares that can be rotated in 4 different orientations

Your challenge is to find the total number of ways to completely tile a 2×n board using these pieces. Every single square on the board must be covered, and no tiles can overlap or extend beyond the board boundaries.

Goal: Given an integer n, return the number of distinct ways to tile the 2×n board.

Note: Since the answer can be astronomically large, return it modulo 10^9 + 7. Two tilings are considered different if there exists at least one position where one tiling has a tile occupying a square that the other tiling doesn't.

Input & Output

example_1.py — Small Board (n=3)
$ Input: n = 3
Output: 5
💡 Note: For a 2×3 board, there are 5 ways: 3 vertical dominoes, 1 horizontal pair + 1 vertical, and 2 tromino-based configurations.
example_2.py — Minimal Case (n=1)
$ Input: n = 1
Output: 1
💡 Note: Only one way to tile a 2×1 board: place a single vertical domino.
example_3.py — Medium Board (n=4)
$ Input: n = 4
Output: 11
💡 Note: A 2×4 board can be tiled in 11 different ways using various combinations of dominoes and trominos.

Constraints

  • 1 ≤ n ≤ 1000
  • Answer must be returned modulo 109 + 7
  • Board dimensions are always 2 × n

Visualization

Tap to expand
Vertical DominoesHorizontal DominoesTromino ShapeState Transition Formuladp[i] = dp[i-1] + dp[i-2] + 2×partial[i-1]partial[i] = dp[i-2] + partial[i-1]• dp[i]: ways to completely fill column i• partial[i]: ways to partially fill column i• All calculations mod 10⁹ + 7Time: O(n), Space: O(1)
Understanding the Visualization
1
Identify Tile Types
Recognize the available domino and tromino pieces and their orientations
2
Define Column States
Each column can be in a 'complete' or 'partial' state based on previous tiles
3
Calculate Transitions
Determine how each state can lead to valid next states
4
Build Solution
Use dynamic programming to count total valid tilings
Key Takeaway
🎯 Key Insight: Instead of trying all possible tile placements, we track column states and use mathematical transitions to build the solution efficiently in linear time.
Asked in
Google 42 Microsoft 38 Amazon 29 Meta 25
42.0K Views
Medium Frequency
~18 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen