62. Unique Paths - Detailed Explanation

Problem statement

You are given two integers, m and n, which represent the dimensions of an m x n grid. A robot is initially located in the top-left corner of this grid (cell (0, 0)). The robot can only move either down or right at any point in time. The goal is for the robot to reach the bottom-right corner of the grid (cell (m - 1, n - 1)). Your task is to determine how many unique paths exist for the robot to get from the top-left corner to the bottom-right corner.

Example 1

  • Input:
    m = 3, n = 7
  • Output:
    28
  • Explanation:
    There are 28 distinct ways for the robot to reach the bottom-right corner by only moving right or down.

Example 2

  • Input:
    m = 3, n = 2
  • Output:
    3
  • Explanation:
    The three unique paths are:
    1. Right → Down → Down
    2. Down → Down → Right
    3. Down → Right → Down

Example 3

  • Input:
    m = 7, n = 3
  • Output:
    28
  • Explanation:
    Similarly, there are 28 distinct ways for the robot to move from (0,0) to (6,2) using only right and down moves.

Hints and intuition

  • Hint 1:
    Think about breaking the question into subproblems. The number of ways to reach any given cell is the sum of the ways to reach the cell above it and the ways to reach the cell on its left.

  • Hint 2:
    Consider using dynamic programming (DP) to build up a solution for the entire grid. Alternatively, notice that the robot must make exactly (m-1) down moves and (n-1) right moves in some order, which leads to a combinatorial solution.

Hint 1 is the whole pattern in one line. Unique Paths is a multidimensional dynamic programming question, built on a grid table rather than a take-or-skip choice. Every member of that family has the same shape. You fill a table, and each entry combines a fixed set of earlier entries, one per allowed choice. Knapsack offers two choices, take the item or skip it. Here the two choices that reach a cell are arrive from above and arrive from the left. Recognizing that shape is worth more than memorizing this one answer, which is why most short lists of recurring coding interview patterns put table-building DP near the top.

Detailed approaches

Approach A: dynamic programming

Idea

  • Create a 2D DP array dp where dp[i][j] represents the number of unique paths to cell (i, j).
  • Base cases:
    • The starting cell dp[0][0] is 1 (only one way to be at the start).
    • For the first row (i = 0), there is only one way to move (all rights) so dp[0][j] = 1.
    • For the first column (j = 0), there is only one way to move (all downs) so dp[i][0] = 1.
  • Recurrence:
    For any cell (i, j), the number of paths to reach it is:
    dp[i][j] = dp[i-1][j] + dp[i][j-1]
    This holds because the robot can only arrive from the top or from the left.

Walkthrough

  1. Initialization:
    Create a grid dp of size m x n. Initialize the first row and first column to 1.
  2. Fill the DP table:
    For each cell (i, j) starting from (1, 1), compute dp[i][j] using the recurrence.
  3. Final answer:
    The value at dp[m-1][n-1] is the number of unique paths from the start to the finish.

Every cell is computed once and read at most twice, so the table never revisits work. That is the payoff of the DP formulation, and it is the same machinery that drives harder grid questions such as Edit Distance, where each cell combines three neighbors instead of two.

Approach B: combinatorial mathematics

Idea

  • To reach the bottom-right corner, the robot must make exactly (m-1) downs and (n-1) rights in any order.
  • The total number of moves required is (m-1 + n-1) = (m+n-2).
  • Counting paths therefore means counting the distinct arrangements of those moves, which the binomial coefficient gives directly:
    Number of unique paths = C(m+n-2, m-1) = (m+n-2)! / [(m-1)! * (n-1)!]

Walkthrough

  1. Total moves:
    Calculate the total number of moves (m+n-2).
  2. Choose moves:
    Choose which moves are downs, or equivalently which are rights. The answer is the number of ways to choose (m-1) moves from (m+n-2).
  3. Final answer:
    Compute the binomial coefficient to get the answer.

Approach B is faster and shorter, but it only works because movement is unrestricted. Add a single blocked cell, as Unique Paths II does, and the formula collapses while the DP table survives with a one-line change. Learn both, and lead with the DP in an interview unless the interviewer asks for the closed form.

Code implementation

Python implementation (dynamic programming)

Python3
Python3

. . . .

Java implementation (dynamic programming)

Java
Java

. . . .

Complexity analysis

Dynamic programming approach

  • Time complexity:
    The solution uses two nested loops to fill the dp table, resulting in O(m * n) time.

  • Space complexity:
    The dp table requires O(m * n) space. (This can be optimized to O(n) by using a single array if needed.)

Combinatorial approach

  • Time complexity:
    Calculating the binomial coefficient can be done in O(m+n) time if optimized properly.
  • Space complexity:
    Only a few variables are needed, so it is O(1).

Common pitfalls and edge cases

Common mistakes

  • Boundary conditions:
    Leaving the first row and first column uninitialized produces wrong counts everywhere downstream.

  • Off-by-one errors:
    Indexing the dp table incorrectly, usually by looping to m or n instead of m-1 or n-1.

  • Integer overflow:
    In the combinatorial approach, computing the factorials outright overflows for larger inputs. Interleaving the multiplications and divisions keeps the running value small and avoids the overflow.

Edge cases

  • Minimum grid size:
    When m = 1 or n = 1, there is only one unique path.
  • Large values:
    Confirm your approach stays fast as m and n grow. The DP approach at O(m*n) time handles moderate grid sizes comfortably.
  • Unique Paths II:
    The same grid, now with obstacles in some cells.
  • Minimum Path Sum:
    Find the path with the minimum sum of values from the top-left to the bottom-right of a grid.
  • Climbing Stairs:
    A one-dimensional version of the same counting recurrence, where you count the ways to reach the top of a staircase.
  • Grid unique paths variations:
    Grids that permit more directions, or that add constraints such as a move budget.

These questions repay practice as a group, not one at a time. Most of them appear on the Blind 75 list, and Grokking the Coding Interview teaches this one in its Unique Paths lesson alongside the other grid and table problems.

TAGS
Coding Interview Questions
Coding Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
How do I delete a commit from a branch?
Three ways to remove a commit from a Git branch, which one is safe on a shared branch, and how to recover if you delete the wrong one.
What is the importance of software engineering?
What is your weakness as a PM?
What Is AA in a Microsoft Interview?
AA stands for As Appropriate. It is an extra round with a senior leader from outside the hiring team, and that leader can reject a candidate the loop supported.
What is the project life cycle?
What are the three types of data engineers?
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.6
(3,192 learners)
Discounted price for Your Region

$98

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
About Us
Our Team
Careers
Partners
Contact Us
Become Affiliate
Copyright © 2026 Design Gurus, LLC. All rights reserved.