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 C++ code that calculates unique paths in a 3x3 grid?
DSA C++
int uniquePaths(int m, int n) { int dp[3][3] = {0}; 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]; } int main() { int result = uniquePaths(3, 3); std::cout << result << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Think about how many ways you can move right or down to reach the bottom-right corner in a 3x3 grid.
✗ Incorrect
The number of unique paths in a 3x3 grid moving only right or down is 6. The dp table counts all ways to reach each cell by summing ways from the top and left cells.
🧠 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 cells can only be reached by moving right from the start, so only one path exists. Similarly, the first column cells can only be reached by moving down, so only one path exists.
❓ Predict Output
advancedOutput with Obstacle Grid
What is the output of the following C++ code that calculates unique paths in a 3x3 grid with an obstacle at position (1,1)?
DSA C++
int uniquePathsWithObstacles(int obstacleGrid[3][3]) { int dp[3][3] = {0}; if (obstacleGrid[0][0] == 1) return 0; dp[0][0] = 1; for (int i = 1; i < 3; i++) { dp[i][0] = (obstacleGrid[i][0] == 0 && dp[i-1][0] == 1) ? 1 : 0; } for (int j = 1; j < 3; j++) { dp[0][j] = (obstacleGrid[0][j] == 0 && dp[0][j-1] == 1) ? 1 : 0; } for (int i = 1; i < 3; i++) { for (int j = 1; j < 3; j++) { if (obstacleGrid[i][j] == 0) { dp[i][j] = dp[i-1][j] + dp[i][j-1]; } else { dp[i][j] = 0; } } } return dp[2][2]; } int main() { int grid[3][3] = {{0,0,0},{0,1,0},{0,0,0}}; int result = uniquePathsWithObstacles(grid); std::cout << result << std::endl; return 0; }
Attempts:
2 left
💡 Hint
The obstacle blocks some paths, reducing the total number of unique paths.
✗ Incorrect
The obstacle at (1,1) blocks paths passing through that cell. The dp table counts only paths avoiding the obstacle. The total unique paths to bottom-right is 2.
🔧 Debug
advancedIdentify the Bug in DP Implementation
The following code is intended to calculate unique paths in an m x n grid but produces incorrect results. What is the bug?
DSA C++
int uniquePaths(int m, int n) { int dp[m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (i == 0 || j == 0) dp[i][j] = 1; else dp[i][j] = dp[i-1][j] * dp[i][j-1]; } } return dp[m-1][n-1]; }
Attempts:
2 left
💡 Hint
Think about how the number of paths to a cell is calculated from its neighbors.
✗ Incorrect
The number of unique paths to a cell is the sum of paths from the cell above and the cell to the left, not the product.
🚀 Application
expertNumber of Unique Paths in a Large Grid
You want to find the number of unique paths in a 20x20 grid moving only right or down. Which approach is best to compute this efficiently?
Attempts:
2 left
💡 Hint
Think about combinatorics and how many moves are needed to reach the bottom-right corner.
✗ Incorrect
The number of unique paths in an m x n grid is the binomial coefficient C(m+n-2, m-1). For large grids, computing this directly is more efficient than DP or recursion.
