Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: How many paths go from (0,0) to (4,4) on a 5 x 5 grid using right and down moves while avoiding cell (2,2)? Run the DP.

Problem: How many paths go from (0,0) to (4,4) on a 5 x 5 grid using right and down moves while avoiding cell (2,2)? Run the DP.
n = 5
g = [[0] * n for _ in range(n)]
g[0][0] = 1
for i in range(n):
    for j in range(n):
        if (i, j) == (0, 0):
            continue
        if (i, j) == (2, 2):
            g[i][j] = 0
            continue
        g[i][j] = (g[i - 1][j] if i else 0) + (g[i][j - 1] if j else 0)
print(g[4][4])
What is printed?
  1. A. 70
  2. B. 36
  3. C. 64
  4. D. 34

Correct answer: D. 34

C(8,4) = 70 paths minus C(4,2) · C(4,2) = 36 through (2,2) leaves 34. Forgetting the block gives 70, 36 counts the bad paths, and subtracting only 6 gives 64.

More Coding Interview Problems questions

Exams that ask this topic