Challenge - 5 Problems
Minimum Path Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Minimum Path Sum Calculation
What is the output of the following Go code that calculates the minimum path sum in a 3x3 grid?
DSA Go
package main import "fmt" func minPathSum(grid [][]int) int { m, n := len(grid), len(grid[0]) for i := 1; i < m; i++ { grid[i][0] += grid[i-1][0] } for j := 1; j < n; j++ { grid[0][j] += grid[0][j-1] } for i := 1; i < m; i++ { for j := 1; j < n; j++ { if grid[i-1][j] < grid[i][j-1] { grid[i][j] += grid[i-1][j] } else { grid[i][j] += grid[i][j-1] } } } return grid[m-1][n-1] } func main() { grid := [][]int{{1,3,1},{1,5,1},{4,2,1}} fmt.Println(minPathSum(grid)) }
Attempts:
2 left
💡 Hint
Trace the path from top-left to bottom-right, adding the smallest sums step by step.
✗ Incorrect
The minimum path sum is calculated by dynamic programming. The path is 1 -> 3 -> 1 -> 1 -> 1 with sum 7.
❓ Predict Output
intermediateMinimum Path Sum with Negative Values
What is the output of this Go code for minimum path sum when the grid contains negative values?
DSA Go
package main import "fmt" func minPathSum(grid [][]int) int { m, n := len(grid), len(grid[0]) for i := 1; i < m; i++ { grid[i][0] += grid[i-1][0] } for j := 1; j < n; j++ { grid[0][j] += grid[0][j-1] } for i := 1; i < m; i++ { for j := 1; j < n; j++ { if grid[i-1][j] < grid[i][j-1] { grid[i][j] += grid[i-1][j] } else { grid[i][j] += grid[i][j-1] } } } return grid[m-1][n-1] } func main() { grid := [][]int{{1,-3,1},{1,5,-1},{4,2,1}} fmt.Println(minPathSum(grid)) }
Attempts:
2 left
💡 Hint
Negative values can reduce the path sum. Follow the minimum sums carefully.
✗ Incorrect
The minimum path sum is -1 following path 1 -> -3 -> 1 -> -1 -> 1.
🔧 Debug
advancedIdentify the Error in Minimum Path Sum Code
What error will this Go code produce when run?
DSA Go
package main import "fmt" func minPathSum(grid [][]int) int { m, n := len(grid), len(grid[0]) for i := 1; i < m; i++ { grid[i][0] += grid[i-1][0] } for j := 1; j < n; j++ { grid[0][j] += grid[0][j-1] } for i := 1; i < m; i++ { for j := 1; j < n; j++ { if grid[i-1][j] < grid[i][j-1] { grid[i][j] += grid[i-1][j] } else { grid[i][j] += grid[i][j-1] } } } return grid[m-1][n-1] } func main() { grid := [][]int{{1,3,1},{1,5,1},{4,2,1}} fmt.Println(minPathSum(grid)) }
Attempts:
2 left
💡 Hint
Check the loop conditions carefully for array indexing.
✗ Incorrect
The loops use <= which causes index to go out of bounds causing runtime panic.
🧠 Conceptual
advancedWhy Dynamic Programming Works for Minimum Path Sum
Why is dynamic programming a suitable approach to solve the minimum path sum in a grid problem?
Attempts:
2 left
💡 Hint
Think about how the problem can be divided into smaller parts.
✗ Incorrect
Dynamic programming solves problems by breaking them into smaller overlapping subproblems and storing their solutions to avoid repeated work.
🚀 Application
expertMinimum Path Sum with Obstacles
Given a grid where some cells are obstacles (represented by -1), which code modification correctly computes the minimum path sum avoiding obstacles? Assume movement only right or down.
DSA Go
package main import "fmt" func minPathSumWithObstacles(grid [][]int) int { m, n := len(grid), len(grid[0]) for i := 0; i < m; i++ { for j := 0; j < n; j++ { if grid[i][j] == -1 { continue } if i == 0 && j == 0 { continue } up := 1<<31 - 1 left := 1<<31 - 1 if i > 0 && grid[i-1][j] != -1 { up = grid[i-1][j] } if j > 0 && grid[i][j-1] != -1 { left = grid[i][j-1] } minPrev := up if left < up { minPrev = left } if minPrev == 1<<31 - 1 { grid[i][j] = -1 } else { grid[i][j] += minPrev } } } if grid[m-1][n-1] == -1 { return -1 } return grid[m-1][n-1] } func main() { grid := [][]int{{1,3,1},{1,-1,1},{4,2,1}} fmt.Println(minPathSumWithObstacles(grid)) }
Attempts:
2 left
💡 Hint
Observe how obstacles affect reachability and path sums.
✗ Incorrect
The obstacle at grid[1][1] does not block all paths to bottom-right, so the function computes the minimum path sum of 7.
