Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the first cell of the dp grid with the grid's first cell value.
DSA Go
dp[0][0] = grid[1][0][0]
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' or '*' instead of indexing brackets.
Trying to assign dp[0][0] a value without indexing grid.
✗ Incorrect
We assign dp[0][0] the value of grid[0][0] to start the minimum path sum calculation.
2fill in blank
mediumComplete the code to fill the first row of dp by adding the current grid cell to the previous dp cell.
DSA Go
for j := 1; j < cols; j++ { dp[0][j] = dp[0][j-1] [1] grid[0][j] }
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Using wrong indices for dp or grid.
✗ Incorrect
We add the current grid cell value to the previous dp cell to accumulate the path sum along the first row.
3fill in blank
hardFix the error in the nested loop to correctly compute dp[i][j] as the minimum path sum from top or left plus current grid value.
DSA Go
for i := 1; i < rows; i++ { for j := 1; j < cols; j++ { dp[i][j] = min(dp[i-1][j], dp[i][j-1]) [1] grid[i][j] } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Forgetting to add the current grid cell value.
✗ Incorrect
We add the current grid cell value to the minimum of the top and left dp cells to get the minimum path sum at dp[i][j].
4fill in blank
hardFill both blanks to define the min function and use it to find the smaller of two integers.
DSA Go
func min(a, b int) int { if a [1] b { return a } return [2] }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' in the if condition.
Returning 'a' in the else case instead of 'b'.
✗ Incorrect
The min function returns a if a is less than b; otherwise, it returns b.
5fill in blank
hardFill all three blanks to complete the function that returns the minimum path sum from top-left to bottom-right in the grid.
DSA Go
func minPathSum(grid [][]int) int {
rows, cols := len(grid), len(grid[0])
dp := make([][]int, rows)
for i := 0; i < rows; i++ {
dp[i] = make([]int, cols)
}
dp[0][0] = grid[0][0]
for j := 1; j < cols; j++ {
dp[0][j] = dp[0][j-1] [1] grid[0][j]
}
for i := 1; i < rows; i++ {
dp[i][0] = dp[i-1][0] [2] grid[i][0]
}
for i := 1; i < rows; i++ {
for j := 1; j < cols; j++ {
dp[i][j] = min(dp[i-1][j], dp[i][j-1]) [3] grid[i][j]
}
}
return dp[rows-1][cols-1]
}Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Forgetting to add grid values to dp cells.
✗ Incorrect
All additions accumulate the path sums correctly: first row, first column, and inner cells.
