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
What is the output of the following code that calculates unique paths in a 3x3 grid?
DSA Javascript
function uniquePaths(m, n) {
const dp = 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));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 is 6 because you can move right or down in different sequences to reach the end.
🧠 Conceptual
intermediateUnderstanding DP Table Initialization
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
The first row and column can only be reached by moving straight right or straight down respectively, so there is exactly one path to each of those cells.
❓ Predict Output
advancedOutput with Obstacles in Grid
What is the output of the code below that calculates unique paths in a 3x3 grid with an obstacle at position (1,1)?
DSA Javascript
function uniquePathsWithObstacles(obstacleGrid) {
const m = obstacleGrid.length;
const n = obstacleGrid[0].length;
const dp = Array(m).fill().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));Attempts:
2 left
💡 Hint
The obstacle blocks some paths, reducing the total count.
✗ Incorrect
With an obstacle in the middle, only two unique paths remain to reach the bottom-right corner.
🔧 Debug
advancedIdentify the Error in DP Implementation
What error will the following code produce when calculating unique paths in a 3x3 grid?
DSA Javascript
function uniquePaths(m, n) {
const dp = Array(m).fill(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));Attempts:
2 left
💡 Hint
Check how the dp array is initialized and whether rows are independent.
✗ Incorrect
Using Array(m).fill(Array(n).fill(1)) makes all rows point to the same array, so updating one row updates all rows, causing wrong results.
🚀 Application
expertNumber of Unique Paths in a 5x7 Grid
How many unique paths are there from the top-left to the bottom-right corner in a 5x7 grid if you can only move right or down?
Attempts:
2 left
💡 Hint
Use the formula for combinations: (m+n-2) choose (m-1).
✗ Incorrect
The number of unique paths is the binomial coefficient C(5+7-2, 5-1) = C(10,4) = 210, but options show 210 as C, so check carefully. Actually, C(10,4) = 210, but the grid is 5x7, so m=5, n=7, total moves = 5-1 + 7-1 = 10 moves, choose 4 downs or 6 rights. C(10,4) = 210. So correct answer is 210 (option D).
