Unique Paths (DP on Grid)
A medium Dynamic Programming interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- Dynamic Programming
- Sheets
- 1
- Core for
- 9 roles
The problem
A robot starts at the top-left corner of a grid and can only move down or right. How many unique paths lead to the bottom-right corner? This variant asks for the count on an m x n grid.
Example 1
- Input
- m = 3, n = 2
- Output
- 3
- Why
- Three unique paths: right-down-down, down-right-down, down-down-right.
Example 2
- Input
- m = 7, n = 3
- Output
- 28
- Why
- There are 28 unique paths in a 7x3 grid.
Example 3
- Input
- m = 3, n = 3
- Output
- 6
- Why
- Six unique paths from top-left to bottom-right in a 3x3 grid.
Constraints
- 1 <= m, n <= 100
- The answer will be less than or equal to 2 * 10^9
How to think about it
Updated 2026-09-29Every path requires making exactly m - 1 down moves and n - 1 right moves in any arbitrary sequence. Because every path is an arrangement of these identical steps, the problem is pure combinations: choose m - 1 down steps out of total (m + n - 2) moves.
Approaches, worst first
Recursive path counting
time O(2^(m+n)) · space O(m + n)
From cell (r, c), return paths(r+1, c) + paths(r, c+1) and stop at the edges. The recursion mirrors the grid exactly, but every interior cell is reachable by many different routes from the start, so each one is recomputed once per route rather than once in total.
1D rolling DP row
time O(m * n) · space O(n)
Initialize an array of size n to 1. For each row from 1 to m - 1, sweep j from 1 to n - 1 updating dp[j] += dp[j-1]. Accumulating transitions row by row consumes linear space and polynomial runtime, yet still falls behind the constant-space closed-form combinatorial approach.
Direct combination formulaWrite this one
time O(min(m, n)) · space O(1)
Calculate C(m + n - 2, min(m - 1, n - 1)) using iterative multiplication and division, multiplying factors one by one to prevent intermediate overflow.
Where people lose marks · 3
- Integer overflow during combinatorics: multiplying all terms in the numerator before dividing exceeds 64-bit integer limits; perform divisions incrementally during the loop.
- Boundary case where m = 1 or n = 1: with only a single row or column, exactly 1 path exists.
- Using floating-point divisions: introduces rounding errors on large grid sizes.
The theory behind it
Dynamic Programming — the ground this problem stands on. All Dynamic Programming problems
What Dynamic Programming is
Dynamic programming is a method for solving a complex problem by breaking it into overlapping subproblems, solving each subproblem only once, and remembering the answers in a lookup table. Instead of recalculating identical questions over and over, future steps look up previous answers directly. By assembling these saved pieces from the bottom up or storing them during recursion, a task that would take billions of steps finishes in a fraction of a second.
When to reach for it
Reach for dynamic programming when questions ask for the maximum profit, minimum cost, total number of distinct ways to achieve a goal, or whether a target can be formed. Signals include overlapping choices where making a choice now affects what choices remain later, but greedy picking fails to find the true global optimum. If drawing a recursive decision tree reveals the same subproblem states repeating across branches, dynamic programming is needed.
How the pattern works
Identify the state variables that uniquely describe a subproblem, such as an array index and remaining capacity. Write the base cases first, representing states whose answers are known without calculation. Next, write the recurrence relation that expresses the current state using previously solved states, taking the minimum, maximum, or sum among your options. Build the solution either top-down by caching recursive returns in a memo table, or bottom-up by filling an array in topological dependency order. When each state depends only on the previous row, compress storage down to a single array.
More Dynamic Programming problems
Target Roles
Core requirement for 9 roles:
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .