Unique Paths — Coding Interview Walkthrough
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.
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
Already comfortable with the solution? Practice it in a mock interview →
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.
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