📖 2 min read
Last updated on

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

SkillWhat Interviewers Observe
DP recurrenceCan you define dp[r][c] and its recurrence?
Space optimizationCan you reduce from 2D to 1D DP?
Combinatorics insightDo 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).

3×3 DP grid showing unique path counts: top-left=1, bottom-right=6, each cell dp[r][c] = dp[r-1][c] + dp[r][c-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

ApproachTimeSpace
1D DPO(m × n)O(n)
CombinatoricsO(min(m,n))O(1)

Common Interview Mistakes

  1. Forgetting base cases. First row and column are all 1s — only one way to reach any edge cell.
  2. Not knowing the combinatorics formula. Mentioning C(m+n-2, m-1) even if you implement DP is a strong signal.
  3. 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.”



Practice This in a Mock Interview

Start a mock interview for Unique Paths on Intervu.


Further reading:

Practice Like It's the Real Interview

Get instant feedback on your approach, communication, and code — powered by AI.

Start a Mock Interview →