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 = 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]; } Console.WriteLine(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 only move right or down, and the total moves are fixed. The DP table sums paths from top and left cells.
🧠 Conceptual
intermediateUnderstanding DP Table Initialization
Why do we initialize the first row and first column of the DP table with 1s 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 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) { int m = obstacleGrid.GetLength(0); int n = obstacleGrid.GetLength(1); int[,] dp = new int[m, n]; if (obstacleGrid[0, 0] == 1) return 0; dp[0, 0] = 1; for (int i = 1; i < m; i++) { dp[i, 0] = (obstacleGrid[i, 0] == 0 && dp[i - 1, 0] == 1) ? 1 : 0; } for (int j = 1; j < n; j++) { dp[0, j] = (obstacleGrid[0, j] == 0 && dp[0, j - 1] == 1) ? 1 : 0; } for (int i = 1; i < m; i++) { for (int 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]; } int[,] grid = new int[,] { {0, 0, 0}, {0, 1, 0}, {0, 0, 0} }; Console.WriteLine(UniquePathsWithObstacles(grid));
Attempts:
2 left
💡 Hint
The obstacle blocks some paths, reducing the total count.
✗ Incorrect
The obstacle at (1,1) blocks paths passing through that cell. The DP table counts only paths avoiding obstacles, resulting in 2 unique paths.
🔧 Debug
advancedIdentify the Bug in DP Implementation
What error will this code produce when calculating unique paths for a 3x3 grid?
DSA C#
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]; } Console.WriteLine(UniquePaths(3, 3));
Attempts:
2 left
💡 Hint
Check the loop conditions for array indexing.
✗ Incorrect
The loops use <= which causes index to go out of bounds since arrays are zero-indexed and length is m or n.
🚀 Application
expertNumber of Unique Paths in a 5x5 Grid with Obstacles
Given the following 5x5 grid with obstacles (1 represents an obstacle, 0 is free), what is the number of unique paths from top-left to bottom-right moving only right or down?
DSA C#
int[,] grid = new int[,] { {0, 0, 0, 0, 0}, {0, 1, 0, 1, 0}, {0, 0, 0, 0, 0}, {1, 0, 1, 0, 0}, {0, 0, 0, 1, 0} }; int UniquePathsWithObstacles(int[,] obstacleGrid) { int m = obstacleGrid.GetLength(0); int n = obstacleGrid.GetLength(1); int[,] dp = new int[m, n]; if (obstacleGrid[0, 0] == 1) return 0; dp[0, 0] = 1; for (int i = 1; i < m; i++) { dp[i, 0] = (obstacleGrid[i, 0] == 0 && dp[i - 1, 0] == 1) ? 1 : 0; } for (int j = 1; j < n; j++) { dp[0, j] = (obstacleGrid[0, j] == 0 && dp[0, j - 1] == 1) ? 1 : 0; } for (int i = 1; i < m; i++) { for (int 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]; } Console.WriteLine(UniquePathsWithObstacles(grid));
Attempts:
2 left
💡 Hint
Carefully count paths avoiding obstacles using the DP approach.
✗ Incorrect
The DP table counts paths avoiding obstacles. After filling, the bottom-right cell shows 7 unique paths.
