Unique Paths — Coding Interview Walkthrough
The core approach: Unique Paths is solved with dynamic programming in O(m×n) time and O(m×n) space. Each cell's value equals the sum of paths from the cell above and the cell to the left. The top row and left column are all 1s (only one way to reach each). Space can be reduced to O(n) by reusing a single row.
You’re in a coding interview. The interviewer says: “A robot can only move right or down on an m × n grid. How many unique paths from the top-left to the bottom-right?” This is a classic 2D DP problem — but the sharper answer recognizes it’s a combinatorics problem solvable in O(1) space.
Already know how to solve Unique Paths? Try a Unique Paths mock interview on Intervu →
The Problem
There is a robot on an m x n grid. It can only move right or down. Return the number of unique paths from the top-left to the bottom-right corner.
Example
Input: m = 3, n = 7
Output: 28
What Interviewers Are Actually Testing
| Skill | What Interviewers Observe |
|---|---|
| DP recurrence | Can you define dp[r][c] and its recurrence? |
| Space optimization | Can you reduce from 2D to 1D DP? |
| Combinatorics insight | Do you recognize this as C(m+n-2, m-1)? |
Step 1: The Key Insight
Every path uses exactly (m-1) down moves and (n-1) right moves. The number of unique paths = the number of ways to arrange these moves = C(m+n-2, m-1).
Python Implementation
1D DP (space-optimized):
def uniquePaths(m: int, n: int) -> int:
dp = [1] * n
for _ in range(1, m):
for c in range(1, n):
dp[c] += dp[c-1]
return dp[n-1]
Combinatorics (O(1) space):
from math import comb
def uniquePaths(m: int, n: int) -> int:
return comb(m + n - 2, m - 1)
Time & Space Complexity
| Approach | Time | Space |
|---|---|---|
| 1D DP | O(m × n) | O(n) |
| Combinatorics | O(min(m,n)) | O(1) |
Common Interview Mistakes
- Forgetting base cases. First row and column are all 1s — only one way to reach any edge cell.
- Not knowing the combinatorics formula. Mentioning
C(m+n-2, m-1)even if you implement DP is a strong signal. - Confusing m and n. Keep row/column indexing consistent.
What a Strong Candidate Sounds Like
“The DP recurrence is
dp[r][c] = dp[r-1][c] + dp[r][c-1]. I can space-optimize to O(n) with a 1D array. The mathematical insight is that every path is(m-1)downs and(n-1)rights interleaved —C(m+n-2, m-1)combinations.”
Related Problems to Practice
- Climbing Stairs — 1D version of the same recurrence.
- Coin Change — DP with optimization choices.
- Maximum Subarray — DP pattern recognition.
Frequently Asked Questions
What defines the recursive logic for tracking grid Unique Paths?
Any generic intermediate grid coordinate is exclusively reachable mathematically from exactly two potential directions: the immediate square above it, and the immediate square perfectly to its left. Thus, the total unique paths reaching that cell simply totals the paths of those two valid neighbors.
Can we optimize the Dynamic Programming grid memory constraints?
Yes, while modeling an entire 2D matrix mimics grid dimensions perfectly, calculating the upcoming path totals only strictly requires remembering the single current evaluation row. Stripping the matrix history dramatically bounds required mathematical tracking variables to pure O(N) spatial limits.
Practice This in a Mock Interview
Start a mock interview for Unique Paths on Intervu.
Further reading:
- How to Prepare for a Coding Interview in 2026, the complete roadmap
- Why LeetCode alone isn’t enough, and what to practice instead
- Practice any LeetCode problem as a live mock interview
- The Grind 75 Pathway, a structured study plan with AI practice links
- Browse all walkthroughs on GitHub, condensed solutions with code