Data Structures & AlgorithmsVery hardTCS CodeVita · Microsoft SDE OA

How many ways are there to tile a 3 × 8 board completely with 1 × 2 dominoes (placed horizontally or vertically)? Count by DFS over the first empty cell.

How many ways are there to tile a 3 × 8 board completely with 1 × 2 dominoes (placed horizontally or vertically)? Count by DFS over the first empty cell.
W,H=8,3
g=[[0]*W for _ in range(H)]
def go(pos):
    if pos==W*H: return 1
    r,c=divmod(pos,W)
    if g[r][c]: return go(pos+1)
    tot=0
    if c+1<W and not g[r][c+1]:
        g[r][c]=g[r][c+1]=1; tot+=go(pos+1); g[r][c]=g[r][c+1]=0
    if r+1<H and not g[r+1][c]:
        g[r][c]=g[r+1][c]=1; tot+=go(pos+1); g[r][c]=g[r+1][c]=0
    return tot
print(go(0))
What is printed?
  1. A. 153
  2. B. 2245
  3. C. 34
  4. D. 140

Correct answer: A. 153

The DFS over the first free cell enumerates every domino tiling of the 3×8 board, giving the known count 153. A 4×8 board has far more tilings, a 2×8 board has 34, and a binomial product does not describe this tiling.

More Coding Interview Problems questions

Exams that ask this topic