Challenge - 5 Problems
Unique Paths Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Unique Paths Calculation in 3x3 Grid
What is the output of the following Java code that calculates unique paths in a 3x3 grid?
DSA Java
public class UniquePaths { public static int uniquePaths(int m, int n) { int[][] dp = new int[m][n]; for (int i = 0; i < m; i++) { dp[i][0] = 1; } for (int j = 0; j < n; j++) { dp[0][j] = 1; } for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { dp[i][j] = dp[i-1][j] + dp[i][j-1]; } } return dp[m-1][n-1]; } public static void main(String[] args) { System.out.println(uniquePaths(3, 3)); } }
Attempts:
2 left
💡 Hint
Think about how many ways you can move right or down to reach the bottom-right corner.
✗ Incorrect
The number of unique paths in a 3x3 grid moving only right or down is 6. The dp table counts all possible ways to reach each cell by summing ways from the top and left cells.
🧠 Conceptual
intermediateUnderstanding Base Cases in Unique Paths DP
Why do we initialize the first row and first column of the DP table with 1 in the Unique Paths problem?
Attempts:
2 left
💡 Hint
Think about how you can move from the start to cells in the first row or column.
✗ Incorrect
You can only move right or down. To reach any cell in the first row, you must move right all the way. Similarly, to reach any cell in the first column, you must move down all the way. So, there is exactly one path to each of these cells.
🔧 Debug
advancedIdentify the Bug in Unique Paths DP Implementation
What error will the following Java code produce when calculating unique paths for a 3x3 grid?
DSA Java
public class UniquePaths { public static int uniquePaths(int m, int n) { int[][] dp = new int[m][n]; for (int i = 0; i <= m; i++) { dp[i][0] = 1; } for (int j = 0; j <= n; j++) { dp[0][j] = 1; } for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { dp[i][j] = dp[i-1][j] + dp[i][j-1]; } } return dp[m-1][n-1]; } public static void main(String[] args) { System.out.println(uniquePaths(3, 3)); } }
Attempts:
2 left
💡 Hint
Check the loop conditions for initializing the dp array.
✗ Incorrect
The loops initializing the first row and column use <= which causes the index to go out of bounds since arrays are 0-indexed. This throws ArrayIndexOutOfBoundsException.
🚀 Application
advancedNumber of Unique Paths in a 4x5 Grid
What is the number of unique paths from the top-left to bottom-right corner in a 4x5 grid moving only right or down?
Attempts:
2 left
💡 Hint
Use the formula for combinations or the DP approach to count paths.
✗ Incorrect
The number of unique paths in a 4x5 grid moving only right or down is 35. This is calculated using the combinatorial formula C(4+5-2, 4-1) = C(7,3) = 35.
❓ Predict Output
expertOutput of Unique Paths with Obstacles
What is the output of the following Java code that calculates unique paths in a 3x3 grid with an obstacle at position (1,1)?
DSA Java
public class UniquePathsWithObstacles { public static int uniquePathsWithObstacles(int[][] obstacleGrid) { int m = obstacleGrid.length; int n = obstacleGrid[0].length; int[][] dp = new int[m][n]; for (int i = 0; i < m; i++) { if (obstacleGrid[i][0] == 1) break; dp[i][0] = 1; } for (int j = 0; j < n; j++) { if (obstacleGrid[0][j] == 1) break; dp[0][j] = 1; } for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { if (obstacleGrid[i][j] == 1) { dp[i][j] = 0; } else { dp[i][j] = dp[i-1][j] + dp[i][j-1]; } } } return dp[m-1][n-1]; } public static void main(String[] args) { int[][] grid = { {0,0,0}, {0,1,0}, {0,0,0} }; System.out.println(uniquePathsWithObstacles(grid)); } }
Attempts:
2 left
💡 Hint
Consider how the obstacle blocks some paths and how DP accumulates paths avoiding it.
✗ Incorrect
The obstacle at (1,1) blocks paths through that cell. The DP counts paths avoiding it. The total unique paths from top-left to bottom-right are 2.
