Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice

Problem: What is the number of paths from (0,0) to (4,4) using right and up moves that never touch the diagonal cells (i, i) except at the endpoints? Run the DP.

Problem: What is the number of paths from (0,0) to (4,4) using right and up moves that never touch the diagonal cells (i, i) except at the endpoints? Run the DP.
n = 4
dp = [[0] * (n + 1) for _ in range(n + 1)]
dp[0][0] = 1
for i in range(n + 1):
    for j in range(n + 1):
        if (i, j) == (0, 0):
            continue
        if i == j and i > 0 and i < n:
            dp[i][j] = 0
            continue
        dp[i][j] = (dp[i - 1][j] if i else 0) + (dp[i][j - 1] if j else 0)
print(dp[n][n])
What is printed?
  1. A. 70
  2. B. 14
  3. C. 5
  4. D. 10

Correct answer: D. 10

Paths that avoid the interior diagonal stay strictly below or strictly above it, each counted by Catalan C₃ = 5, total 10. No restriction gives 70, 14 is C₄ (touching allowed), and 5 counts one side only.

More Coding Interview Problems questions

Exams that ask this topic