Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the DP array with the number of rows.
DSA C#
int m = grid.GetLength(0); int n = grid.GetLength(1); int[,] dp = new int[[1], n];
Attempts:
3 left
💡 Hint
Common Mistakes
Using n instead of m for the number of rows.
Confusing rows and columns in array dimensions.
✗ Incorrect
The number of rows is stored in variable m, so dp array should have m rows.
2fill in blank
mediumComplete the code to set the base case for the first row in the DP array.
DSA C#
for (int j = 0; j < n; j++) { if (grid[0, j] == 1) break; dp[0, j] = [1]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting dp[0, j] to 0 instead of 1.
Using grid value directly instead of 1.
✗ Incorrect
The first row cells before any obstacle have exactly 1 unique path to reach.
3fill in blank
hardFix the error in the nested loop that fills the DP array for unique paths.
DSA C#
for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { if (grid[i, j] == 1) { dp[i, j] = 0; } else { dp[i, j] = dp[i - 1, j] [1] dp[i, j - 1]; } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Forgetting to handle obstacles by setting dp[i, j] to 0.
✗ Incorrect
The number of unique paths to a cell is the sum of paths from the top and left cells.
4fill in blank
hardFill both blanks to initialize the first column and first row of the DP array correctly.
DSA C#
for (int i = 0; i < m; i++) { if (grid[i, 0] == 1) break; dp[i, 0] = [1]; } for (int j = 0; j < n; j++) { if (grid[0, j] == 1) break; dp[0, j] = [2]; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of 1 for initialization.
Using loop variables i or j as values.
✗ Incorrect
Both first column and first row cells before obstacles have 1 unique path.
5fill in blank
hardFill all three blanks to complete the method that returns the number of unique paths avoiding obstacles.
DSA C#
public int UniquePathsWithObstacles(int[,] grid) {
int m = grid.GetLength(0);
int n = grid.GetLength(1);
int[,] dp = new int[m, n];
for (int i = 0; i < m; i++) {
if (grid[i, 0] == 1) break;
dp[i, 0] = [1];
}
for (int j = 0; j < n; j++) {
if (grid[0, j] == 1) break;
dp[0, j] = [2];
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if (grid[i, j] == 1) {
dp[i, j] = 0;
} else {
dp[i, j] = dp[i - 1, j] [3] dp[i, j - 1];
}
}
}
return dp[m - 1, n - 1];
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of 1 for initialization.
Using subtraction instead of addition for combining paths.
✗ Incorrect
Initialize first row and column with 1 until obstacles, then sum paths from top and left cells.
