Domino and Tromino Tiling solution explained

To count tilings of a 2 by n board with dominoes and L-trominoes, track two states per column: full[i], the first i columns exactly covered, and part[i], the first i - 1 columns covered plus one cell of column i. Then full[i] = full[i - 1] + full[i - 2] + 2 * part[i - 1] and part[i] = part[i - 1] + full[i - 2], which simplifies to f(n) = 2 * f(n - 1) + f(n - 3) with f(0) = 1, f(1) = 1, f(2) = 2. Both run in O(n) time, and the counts are reported modulo 1,000,000,007.

By Published 7 min read

The problem in plain words

You have a board two cells tall and n cells wide. You may use two kinds of pieces as many times as you like: a domino covering two adjacent cells (placed upright or lying flat), and an L-shaped tromino covering three cells of a 2 by 2 square (any of its four rotations). Count the ways to cover every cell exactly once. Two tilings are different when the pieces sit in different places. The count grows by roughly a factor of 2.2 per column, so the answer is reported modulo 1,000,000,007.

Tiling counts from the code on this page (matching the backtracking tiler up to n = 12)
nTilingsnTilings
11653
227117
358258
4119569
524101,255
Tiling counts from the code on this page (matching the backtracking tiler up to n = 12). Scroll sideways on a small screen.

What the interviewer is checking

Pure domino tiling of a 2 by n board is the Fibonacci sequence, and many candidates know that. Adding the tromino forces you to deal with a jagged edge, and the interviewer wants to see you define states for the edge shapes rather than guess a formula from the first few numbers. A clear derivation of the transitions, careful base cases, and the factor of 2 for mirrored shapes are the core. Arriving at the one-line recurrence and being able to justify it algebraically is a strong finish.

Brute force: place pieces until the board is full

Scan for the first empty cell, column by column and top to bottom. Every tiling must cover that cell with some piece in which it is the first cell in this order, so try each piece shape anchored there, recurse, and remove it. When no empty cell remains, count one tiling. Anchoring at the first empty cell means every tiling is produced exactly once. This is exponential, but it is the right way to get trustworthy small values, and it is how the numbers above were checked.

@@domino-and-tromino-tiling/slow.py@@
Python: backtracking tiler, the test reference for small n.

The pattern: DP over the shape of the edge

Build the tiling from left to right and look at the boundary after each step. Because every piece spans at most two columns, the boundary is always either flat (columns 1 to i fully covered) or flat with one extra cell sticking into the next column. Those are the two states. Call them full[i] and part[i], where part[i] means columns 1 to i - 1 are covered plus exactly one cell of column i, say the top one. The bottom version has the same count by mirror symmetry.

How a flat edge at column i can be reached

  • From full[i - 1], add an upright domino in column i.
  • From full[i - 2], add two flat dominoes stacked in columns i - 1 and i.
  • From a jagged edge at column i - 1, add an L piece that fills the missing cell of column i - 1 and both cells of column i. The jagged edge can stick out at the top or the bottom, so this contributes 2 * part[i - 1].

How a jagged edge at column i can be reached

  • From full[i - 2], add an L piece covering both cells of column i - 1 and the top cell of column i.
  • From the opposite jagged edge at column i - 1 (bottom cell sticking out), add a flat domino along the top row covering column i - 1 and column i. That leaves the top of column i sticking out.

Putting it together: full[i] = full[i - 1] + full[i - 2] + 2 * part[i - 1], and part[i] = part[i - 1] + full[i - 2], with full[0] = 1 (the empty board has one tiling) and part[0] = 0. Treat full[-1] as 0.

@@domino-and-tromino-tiling/fast.py@@
Python: the two-state table, and the shorter recurrence it implies.

Where f(n) = 2f(n - 1) + f(n - 3) comes from

Subtract the full equation at i - 1 from the one at i. The domino terms leave full[i - 1] - full[i - 3], and the tromino terms leave 2 * (part[i - 1] - part[i - 2]). By the part equation, part[i - 1] - part[i - 2] is just full[i - 3]. So full[i] - full[i - 1] = full[i - 1] + full[i - 3], which rearranges to full[i] = 2 * full[i - 1] + full[i - 3] for i from 3 on. It reproduces 5, 11, 24 and onward from the seeds 1, 1, 2. Presenting the states first and this identity second is safer than quoting the formula, because a formula without the derivation sounds memorised.

Because each step only reads the previous few values, the JavaScript version keeps three variables. Every stored value is below the modulus, so full is at most about four times the modulus before the remainder, which is exact in a JavaScript number.

@@domino-and-tromino-tiling/fast.js@@
JavaScript: two states in O(1) memory.

Complexity

n is the board width
ApproachTimeExtra memory
Backtracking tilerExponential, about 2.2^n tilings to visitO(n) for the board
Two-state tableO(n)O(n), or O(1) with rolling variables
f(n) = 2f(n - 1) + f(n - 3)O(n)O(1) with three variables
3 by 3 matrix power of that recurrenceO(log n) small matrix productsO(1)
n is the board width. Scroll sideways on a small screen.

Edge cases

  • n = 1: one upright domino, so 1.
  • n = 2: two upright or two flat dominoes, so 2. No L piece fits a 2 by 2 square alone, because it leaves one cell.
  • n = 3: the first width where L pieces appear, as a mirrored pair, giving 5.
  • n = 0, if allowed: one tiling, the empty one. Confirm whether the problem wants 1 or 0.
  • Large n: take the remainder after each step; the true count first exceeds the signed 64-bit maximum at n = 57.

Mistakes to watch for

  • Guessing the recurrence from 1, 2, 5, 11 and stopping there. Several formulas fit four numbers; the states prove the right one.
  • Forgetting the factor of 2 for the two mirror images of the jagged edge.
  • Counting two flat dominoes and two upright dominoes as the same tiling.
  • Off-by-one base cases, especially using full[i - 2] when i is 1.
  • Writing the brute-force tiler without anchoring at the first empty cell, which counts the same tiling several times.

How to explain it out loud

Fictional example

Describing the edge, not the pieces
Candidate
If I fill from the left, the boundary is either flat or has one cell sticking out into the next column. That's my state: full of i and part of i.
Candidate
A flat edge at i comes from one upright domino, two flat dominoes, or an L piece closing a jagged edge, and the jagged edge can be top or bottom, so that term doubles.
Interviewer
Is there a single-sequence version?
Candidate
Yes, subtracting consecutive equations gives f of n equals two f of n minus 1 plus f of n minus 3. Same linear time, three variables.

Knight Dialer is another short linear recurrence that a matrix power can jump ahead. Count Vowels Permutation tracks a handful of states per step, much like full and part here. Fibonacci Number is the dominoes-only version of this board and the place to practice fast exponentiation. Tiling a 3 by n board with dominoes is a harder profile DP worth trying next.

How Natively helps you practice

Tiling problems reward drawing before coding, and explaining the drawing clearly is the skill to rehearse. While practicing on Mac or Windows, Natively can read the board description from a screenshot, check your transitions when a count comes out wrong, and ask you to justify the shorter recurrence. The transcript of the session shows whether the factor of 2 got an explanation or only a mention.

Questions people ask

How do you solve Domino and Tromino Tiling?

Track two states: full[i], the first i columns covered, and part[i], the first i - 1 columns plus one cell of column i. Then full[i] = full[i - 1] + full[i - 2] + 2 * part[i - 1] and part[i] = part[i - 1] + full[i - 2].

What is the recurrence for Domino and Tromino Tiling?

f(n) = 2 * f(n - 1) + f(n - 3) for n of 3 or more, with f(0) = 1, f(1) = 1 and f(2) = 2. It follows from subtracting consecutive equations of the two-state DP.

Why is there a factor of 2 in the tiling DP?

The jagged edge can stick out at the top or at the bottom of the column. Both shapes have the same count by mirror symmetry, so the term for closing it with an L piece is doubled.

What is the time complexity of Domino and Tromino Tiling?

O(n) time and O(1) memory with rolling variables. A 3 by 3 matrix power of the recurrence brings it to O(log n) for very large n.

Where to go next

Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.

Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.

Keep reading

Put it on your own machine.

A live-conversation assistant that runs locally. Your conversations stay on your machine.

macOS 12+ · Windows 10/11

© 2026 Natively AI Private Limited

Source-available under the Natively Personal Use Source License v1.0 · IP geolocation by DB-IP