Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the first row of the DP table with 1s.
DSA C
for (int j = 0; j < n; j++) { dp[0][j] = [1]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting dp[0][j] to 0 instead of 1.
Using the loop variable j as the value.
✗ Incorrect
The first row has only one way to reach each cell by moving right, so dp[0][j] should be set to 1.
2fill in blank
mediumComplete the code to initialize the first column of the DP table with 1s.
DSA C
for (int i = 0; i < m; i++) { dp[i][0] = [1]; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting dp[i][0] to 0 instead of 1.
Using the loop variable i as the value.
✗ Incorrect
The first column has only one way to reach each cell by moving down, so dp[i][0] should be set to 1.
3fill in blank
hardFix the error in the code to correctly compute the number of unique paths for each cell.
DSA C
for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { dp[i][j] = dp[i - 1][j] [1] dp[i][j - 1]; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Using division which is not meaningful here.
✗ Incorrect
The number of unique paths to a cell is the sum of paths from the cell above and the cell to the left, so use '+'.
4fill in blank
hardFill both blanks to complete the nested loops iterating over the grid cells starting from (1,1).
DSA C
for (int [1] = 1; [2] < m; [1]++) { for (int j = 1; j < n; j++) { dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variables in declaration and condition.
Using 'j' for the outer loop which is for columns.
✗ Incorrect
The outer loop variable should be 'i' to iterate rows from 1 to m-1.
5fill in blank
hardFill all three blanks to return the total unique paths from top-left to bottom-right in the grid.
DSA C
int uniquePaths(int m, int n) {
int dp[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] [1] dp[i][j - 1];
}
}
return dp[[2]][[3]];
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' instead of '+' for summing paths.
Returning dp[m][n] which is out of bounds.
✗ Incorrect
Use '+' to sum paths from top and left. Return dp[m-1][n-1] for bottom-right cell.