DSA Tracker

Problem

Medium

Unique Paths

A medium Dynamic Programming interview guide. Task statement, worked examples, intuition, and step-by-step solutions.

Topic
Dynamic Programming
Sheets
3
Core for
9 roles

The problem

A robot is located at the top-left corner of an m x n grid. It can only move either down or right at any point. How many unique paths exist to reach the bottom-right corner?

Example 1

Input
m = 3, n = 7
Output
28
Why
There are 28 unique paths from top-left to bottom-right in a 3x7 grid.

Example 2

Input
m = 3, n = 2
Output
3
Why
Three paths: right-down-down, down-right-down, down-down-right.

Example 3

Input
m = 1, n = 1
Output
1
Why
The robot is already at the destination.

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-29

Every path from start to finish must take exactly m - 1 down moves and n - 1 right moves, for a total of (m - 1) + (n - 1) steps. The number of unique paths is the number of ways to choose which of those total steps are down moves.

Approaches, worst first

  1. Recursive grid traversal

    time O(2^(m+n)) · space O(m + n)

    At cell (r, c), return paths(r+1, c) + paths(r, c+1). Without caching, paths to every interior cell are re-calculated repeatedly across overlapping branches.

  2. 2D dynamic programming grid

    time O(m * n) · space O(m * n)

    Fill an m x n matrix where dp[i][j] = dp[i-1][j] + dp[i][j-1], initializing the top row and leftmost column to 1. Each cell represents paths to that position.

  3. Combinatorics calculationWrite this one

    time O(min(m, n)) · space O(1)

    Compute combination C((m-1) + (n-1), min(m-1, n-1)) directly using iterative multiplication and division. This avoids building any table and completes in linear time relative to dimensions.

Where people lose marks · 3
  • Integer overflow during factorial or numerator calculation in combinatorics. Multiplying out the full numerator before dividing will overflow a standard 64-bit integer even if the final result fits.
  • Off-by-one errors when setting grid bounds. A grid of size 1x1 requires 0 steps and has exactly 1 path.
  • Recomputing combinatorics with floating point arithmetic, which introduces precision drift.

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

Track in your role's order

Pick your target role and all 370 problems resequence to what that interview actually asks.

Start free

Problem set and role mapping as of .