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?- A. 153
- B. 2245
- C. 34
- 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
- Meetings are given as half-open intervals [(0,30),(5,10),(15,20),(12,25),(28,35)]. What is the minimum number of meeting rooms needed so…
- Count the unordered pairs (i < j) of the array a = [1, 2, ..., 14] whose sum is divisible by 3. Run the code. python a=list(range(1,15))…
- Consider the permutation p = [4, 1, 7, 3, 8, 2, 6, 5]. Count its inversions, i.e. pairs of positions i < j with p[i] > p[j]. Run the code…
- Problem: A connected undirected graph has 7 vertices. Its BFS tree from vertex 0 has depths: vertex 0 at depth 0, vertices 1 and 2 at…
- Problem: A graph's vertices 0–4 are connected by edges 0-1 (1), 1-2 (1), 2-3 (1), 3-4 (1), 0-4 (5), 1-3 (3). What is the largest…
- Problem: In a directed graph on vertices 0–3, the arcs are 0→1, 1→2, 2→0, 2→3. The vertex 3 has no outgoing arcs. How many vertices can…