Cover a 2 by n board with dominoes
Reported in interviews at (where)
A board is 2 squares tall and n squares wide. You cover it completely with dominoes, each 2 squares by 1, standing upright or lying flat, with no overlaps and nothing sticking out.
In how many different ways can it be done? The number grows fast, so return it modulo 1,000,000,007.
- Input
n = 1- Output
1
One upright domino.
- Input
n = 3- Output
3
Three upright; one upright then two flat ones stacked; or two flat ones stacked, then one upright.
- Input
n = 4- Output
5
Count them by the left edge: an upright domino leaves a 2 by 3 board (3 ways), two stacked flat ones leave a 2 by 2 board (2 ways).
Limits
1 <= n <= 100000
Hints
Hint 1
Look at the leftmost column. Either one domino stands upright there, or two flat dominoes lie on top of each other and cover the first two columns.
Hint 2
So ways(n) = ways(n - 1) + ways(n - 2), with ways(1) = 1 and ways(2) = 2. Keep the last two values and take the remainder as you go.
Where this was reported
Our own problem on the task these candidates describe; the wording, examples and tests are InternDoor’s.
Run your code to check it on the examples. Submit runs every hidden test.
