Data Structures & AlgorithmsModerateProduct Company SDE · Amazon/Microsoft-style OA

Problem: Count the number of ways to tile a 2 × n board with 2 × 1 dominoes, modulo 10⁹ + 7.

Problem: Count the number of ways to tile a 2 × n board with 2 × 1 dominoes, modulo 10⁹ + 7.
const long long MOD = 1e9 + 7;
long long tile(int n) {
    long long a = 1, b = 1;          // n = 0, n = 1
    for (int i = 2; i <= n; i++) { long long c = (a + b) % MOD; a = b; b = c; }
    return b;
}
// cout << tile(4) << " " << tile(100);
What is printed?
  1. A. 8 782204094
  2. B. 5 782204094
  3. C. 5 687995182
  4. D. 3 782204094

Correct answer: B. 5 782204094

A vertical domino leaves a 2 × (n − 1) board; two horizontal ones leave 2 × (n − 2): Fibonacci again. tile(4) = 5. For n = 100 the value is F(101) mod 10⁹ + 7 = 782204094; taking the modulus each step keeps numbers within long long. O(n) (or O(log n) with matrix power).

More Coding Interview Problems questions

Exams that ask this topic