Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.
Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner.
The test cases are generated so that the answer will be less than or equal to 2 * 109.
Example 1:
Input: m = 3, n = 7 Output: 28
Example 2:
Input: m = 3, n = 2 Output: 3 Explanation: From the top-left corner, there are a total of 3 ways to reach the bottom-right corner: 1. Right -> Down -> Down 2. Down -> Down -> Right 3. Down -> Right -> Down
Constraints:
1 <= m, n <= 100Problem summary: There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time. Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner. The test cases are generated so that the answer will be less than or equal to 2 * 109.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Dynamic Programming
3 7
3 2
unique-paths-ii)minimum-path-sum)dungeon-game)minimum-path-cost-in-a-grid)minimum-cost-homecoming-of-a-robot-in-a-grid)class Solution {
public int uniquePaths(int m, int n) {
// dp[c] = number of ways to reach current row, column c.
int[] dp = new int[n];
java.util.Arrays.fill(dp, 1); // First row: only move right.
for (int r = 1; r < m; r++) {
for (int c = 1; c < n; c++) {
dp[c] += dp[c - 1]; // from top + from left
}
}
return dp[n - 1];
}
}
func uniquePaths(m int, n int) int {
dp := make([]int, n)
for c := 0; c < n; c++ {
dp[c] = 1
}
for r := 1; r < m; r++ {
for c := 1; c < n; c++ {
dp[c] += dp[c-1]
}
}
return dp[n-1]
}
class Solution:
def uniquePaths(self, 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[-1]
impl Solution {
pub fn unique_paths(m: i32, n: i32) -> i32 {
let n = n as usize;
let mut dp = vec![1; n];
for _ in 1..m {
for c in 1..n {
dp[c] += dp[c - 1];
}
}
dp[n - 1]
}
}
function uniquePaths(m: number, n: number): number {
const dp: number[] = Array(n).fill(1);
for (let r = 1; r < m; r++) {
for (let c = 1; c < n; c++) {
dp[c] += dp[c - 1];
}
}
return dp[n - 1];
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.