Count the ways to tile a 2 x n board with dominoes and L-shaped trominoes

Quick 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.

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

You have two kinds of tiles: a `2 x 1` domino and an L-shaped tromino, which covers three cells of a `2 x 2` square. You may rotate both shapes, so a domino can lie vertically or horizontally and a tromino can face any of four directions. Given an integer `n`, return the number of ways to tile a `2 x n` board. In a tiling every cell is covered by exactly one tile. Since the answer may be very large, return it modulo `10^9 + 7`. ### Function Signature ```python def num_tilings(n: int) -> int: ``` ### Rules - Two tilings are different if and only if there are two 4-directionally adjacent cells on the board such that exactly one of the tilings covers both cells with the same tile. - Return the count modulo `10^9 + 7`. ### Constraints - `1 <= n <= 1000` ### Examples ```text Input: n = 3 Output: 5 +---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+ | A | B | C | | A | A | B | | A | B | B | | A | A | B | | A | B | B | +---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+ | A | B | C | | C | C | B | | A | C | C | | A | B | B | | A | A | B | +---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+ +---+---+---+ ``` Cells with the same letter belong to one tile. Three tilings use only dominoes, and the other two use a pair of interlocking trominoes that fill the whole board in one of two mirror-image ways. ```text Input: n = 1 Output: 1 ``` Only one vertical domino fits in a single column. ```text Input: n = 4 Output: 11 ``` Five tilings use only dominoes. Four more put a two-tromino `2 x 3` block (in either mirror form) next to a vertical domino on the left or on the right, and the last two place a tromino at each end with one horizontal domino between them.

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.

Amazon logo
Amazon
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You have two kinds of tiles: a 2 x 1 domino and an L-shaped tromino, which covers three cells of a 2 x 2 square. You may rotate both shapes, so a domino can lie vertically or horizontally and a tromino can face any of four directions.

Given an integer n, return the number of ways to tile a 2 x n board. In a tiling every cell is covered by exactly one tile. Since the answer may be very large, return it modulo 10^9 + 7.

Function Signature

def num_tilings(n: int) -> int:

Rules

  • Two tilings are different if and only if there are two 4-directionally adjacent cells on the board such that exactly one of the tilings covers both cells with the same tile.
  • Return the count modulo 10^9 + 7 .

Constraints

  • 1 <= n <= 1000

Examples

Input: n = 3
Output: 5

+---+---+---+   +---+---+---+   +---+---+---+   +---+---+---+   +---+---+---+
| A | B | C |   | A | A | B |   | A | B | B |   | A | A | B |   | A | B | B |
+---+---+---+   +---+---+---+   +---+---+---+   +---+---+---+   +---+---+---+
| A | B | C |   | C | C | B |   | A | C | C |   | A | B | B |   | A | A | B |
+---+---+---+   +---+---+---+   +---+---+---+   +---+---+---+   +---+---+---+

Cells with the same letter belong to one tile. Three tilings use only dominoes, and the other two use a pair of interlocking trominoes that fill the whole board in one of two mirror-image ways.

Input: n = 1
Output: 1

Only one vertical domino fits in a single column.

Input: n = 4
Output: 11

Five tilings use only dominoes. Four more put a two-tromino 2 x 3 block (in either mirror form) next to a vertical domino on the left or on the right, and the last two place a tromino at each end with one horizontal domino between them.