Magicsheet logo

Number of Ways to Reach Destination in the Grid

Hard
50%
Updated 8/1/2025

Number of Ways to Reach Destination in the Grid

What is this problem about?

The Number of Ways to Reach Destination in the Grid problem asks you to count paths from the top-left to the bottom-right of an n×m grid where you can only move right or down. This is the classic combinatorics problem: the answer is C(n+m-2, n-1) — choosing which steps are "down" from the total steps. The math, combinatorics, and dynamic programming interview pattern is demonstrated.

Why is this asked in interviews?

Uber asks this to test combinatorial reasoning vs DP reasoning. Knowing the closed-form formula C(n+m-2, n-1) demonstrates mathematical insight; being able to derive it via DP shows algorithmic thinking. Both approaches should be presented.

Algorithmic pattern used

Combinatorics formula: Total steps = (n-1) down + (m-1) right = n+m-2 steps. Choose which n-1 are "down" → C(n+m-2, n-1).

DP alternative: dp[i][j] = paths to cell (i,j) = dp[i-1][j] + dp[i][j-1]. Base: dp[0][j] = dp[i][0] = 1. Return dp[n-1][m-1].

Example explanation

n=3, m=3. C(3+3-2, 3-1) = C(4,2) = 6. DP: dp = [[1,1,1],[1,2,3],[1,3,6]]. dp[2][2] = 6. ✓

Common mistakes candidates make

  • Not recognizing the combinatorial formula (using DP without mentioning it).
  • Integer overflow when computing C(n+m-2, n-1) for large n,m (use modular arithmetic).
  • DP off-by-one in base case initialization.
  • Confusing C(n+m-2, n-1) with C(n+m, n) (wrong formula).

Interview preparation tip

Always know both the DP and combinatorial approaches for grid path counting. The formula C(n+m-2, n-1) is elegant and O(min(n,m)) to compute using Pascal's triangle or modular inverse. The DP is O(n*m) and extends to grids with obstacles. For interviews, state both, derive one from first principles, and discuss when each is preferable. Modular inverse computation (for large modulo) is a required companion skill.

Similar Questions