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?- A. 8 782204094
- B. 5 782204094
- C. 5 687995182
- 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
- Problem: Find the minimum number of perfect squares that sum to n. python def squares(n): dp = [0] + [float('inf')] n for i in range(1, n…
- Problem: Count the number of distinct subsequences of s that equal t. python def distinct(s, t): dp = [1] + [0] len(t) for ch in s: for j…
- Problem: Find the longest increasing path in a matrix (moving up/down/left/right to strictly larger values). python from functools import…
- Problem: Find the cheapest way to cut a stick of length n at given positions; each cut costs the current piece's length. python from…
- Problem: Burst balloons; bursting balloon i earns left × a[i] × right with its current neighbours (1 beyond the ends). Maximise coins…
- Problem: Find the minimum number of intervals to remove so that the rest are non-overlapping – implemented with sort by START and keeping…