0
0
DSA Typescriptprogramming~20 mins

Unique Paths in Grid DP in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Unique Paths Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Unique Paths Calculation
What is the output of the following TypeScript code that calculates unique paths in a 3x3 grid?
DSA Typescript
function uniquePaths(m: number, n: number): number {
  const dp: number[][] = Array(m).fill(0).map(() => Array(n).fill(1));
  for (let i = 1; i < m; i++) {
    for (let j = 1; j < n; j++) {
      dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
    }
  }
  return dp[m - 1][n - 1];
}
console.log(uniquePaths(3, 3));
A1
B3
C9
D6
Attempts:
2 left
💡 Hint
Think about how many ways you can reach each cell by moving only right or down.
🧠 Conceptual
intermediate
1:30remaining
Understanding Base Cases in Unique Paths DP
In the Unique Paths dynamic programming solution, why do we initialize the first row and first column with 1s?
ABecause there is only one way to reach any cell in the first row or first column by moving only right or down.
BBecause initializing with 1s reduces the time complexity to O(1).
CBecause the first row and column are never visited in the path calculation.
DBecause the first row and column represent obstacles that block paths.
Attempts:
2 left
💡 Hint
Think about how you can move from the start to cells in the first row or column.
Predict Output
advanced
2:30remaining
Output with Obstacle Grid
What is the output of this TypeScript code that calculates unique paths in a 3x3 grid with an obstacle at position (1,1)?
DSA Typescript
function uniquePathsWithObstacles(obstacleGrid: number[][]): number {
  const m = obstacleGrid.length;
  const n = obstacleGrid[0].length;
  const dp: number[][] = Array(m).fill(0).map(() => Array(n).fill(0));

  dp[0][0] = obstacleGrid[0][0] === 0 ? 1 : 0;

  for (let i = 1; i < m; i++) {
    dp[i][0] = (obstacleGrid[i][0] === 0 && dp[i - 1][0] === 1) ? 1 : 0;
  }

  for (let j = 1; j < n; j++) {
    dp[0][j] = (obstacleGrid[0][j] === 0 && dp[0][j - 1] === 1) ? 1 : 0;
  }

  for (let i = 1; i < m; i++) {
    for (let j = 1; j < n; j++) {
      if (obstacleGrid[i][j] === 0) {
        dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
      } else {
        dp[i][j] = 0;
      }
    }
  }

  return dp[m - 1][n - 1];
}

const grid = [
  [0,0,0],
  [0,1,0],
  [0,0,0]
];

console.log(uniquePathsWithObstacles(grid));
A3
B4
C2
D1
Attempts:
2 left
💡 Hint
Consider how the obstacle blocks some paths.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Unique Paths Code
This TypeScript code is intended to calculate unique paths in a grid but produces incorrect output. What is the bug?
DSA Typescript
function uniquePaths(m: number, n: number): number {
  const dp: number[][] = Array(m).fill([]).map(() => Array(n).fill(1));
  for (let i = 1; i < m; i++) {
    for (let j = 1; j < n; j++) {
      dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
    }
  }
  return dp[m - 1][n - 1];
}
console.log(uniquePaths(3, 3));
AUsing Array(m).fill([]) causes all rows to reference the same array, leading to incorrect updates.
BThe loops should start from 0 instead of 1 to include all cells.
CThe dp array should be initialized with zeros instead of ones.
DThe return statement should be dp[0][0] instead of dp[m - 1][n - 1].
Attempts:
2 left
💡 Hint
Check how the dp array is initialized and how it affects updates.
🚀 Application
expert
3:00remaining
Number of Unique Paths in a 5x7 Grid
Using the standard Unique Paths dynamic programming approach (no obstacles), what is the number of unique paths from the top-left to bottom-right corner in a 5 rows by 7 columns grid?
A330
B210
C792
D462
Attempts:
2 left
💡 Hint
The number of unique paths in an m x n grid is the binomial coefficient C(m+n-2, m-1).