Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the DP array with zeros.
DSA Javascript
const dp = Array(m).fill().map(() => Array(n).fill([1]));Attempts:
3 left
💡 Hint
Common Mistakes
Using 1 instead of 0 causes incorrect path counts.
Using null or undefined causes errors when adding.
✗ Incorrect
We fill the dp array with zeros to represent initial counts of paths.
2fill in blank
mediumComplete the code to set the first row paths to 1.
DSA Javascript
for(let j = 0; j < n; j++) dp[0][j] = [1];
Attempts:
3 left
💡 Hint
Common Mistakes
Setting dp[0][j] to 0 causes no paths counted.
Using j as value causes wrong counts.
✗ Incorrect
The first row can only be reached by moving right, so paths count is 1.
3fill in blank
hardFix the error in the nested loop to fill the dp table.
DSA Javascript
for(let i = 1; i < m; i++) { for(let j = 1; j < [1]; j++) { dp[i][j] = dp[i-1][j] + dp[i][j-1]; } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using m in inner loop causes index errors.
Using i or j as limit causes infinite loops or errors.
✗ Incorrect
The inner loop should run over columns, so it uses n.
4fill in blank
hardFill both blanks to complete the function that returns unique paths.
DSA Javascript
function uniquePaths(m, n) {
const dp = Array(m).fill().map(() => Array(n).fill(0));
for(let i = 0; i < m; i++) dp[i][0] = [1];
for(let j = 0; j < n; j++) dp[0][j] = [2];
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];
}Attempts:
3 left
💡 Hint
Common Mistakes
Setting these to 0 causes no paths counted.
Using i or j as values causes wrong results.
✗ Incorrect
First column and first row cells have exactly one path each.
5fill in blank
hardFill all three blanks to complete the code that calculates unique paths using DP.
DSA Javascript
function uniquePaths(m, n) {
const dp = Array(m).fill().map(() => Array(n).fill(0));
for(let i = 0; i < m; i++) dp[i][0] = [1];
for(let j = 0; j < n; j++) dp[0][j] = [2];
for(let i = 1; i < m; i++) {
for(let j = 1; j < n; j++) {
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 wrong operator instead of + causes wrong counts.
Initializing with 0 causes no paths.
✗ Incorrect
Initialize first row and column with 1, and sum top and left cells for others.
