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 code that calculates the minimum path sum in a 3x3 grid?
DSA Python
def min_path_sum(grid): m, n = len(grid), len(grid[0]) dp = [[0]*n for _ in range(m)] dp[0][0] = grid[0][0] for i in range(1, m): dp[i][0] = dp[i-1][0] + grid[i][0] for j in range(1, n): dp[0][j] = dp[0][j-1] + grid[0][j] for i in range(1, m): for j in range(1, n): dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + grid[i][j] return dp[m-1][n-1] grid = [ [1,3,1], [1,5,1], [4,2,1] ] print(min_path_sum(grid))
Attempts:
2 left
💡 Hint
Trace the path with the smallest sum from top-left to bottom-right.
✗ Incorrect
The minimum path sum is 7 by following the path 1 -> 3 -> 1 -> 1 -> 1.
❓ Predict Output
intermediateMinimum Path Sum with Negative Values
What is the output of this code when the grid contains negative values?
DSA Python
def min_path_sum(grid): m, n = len(grid), len(grid[0]) dp = [[0]*n for _ in range(m)] dp[0][0] = grid[0][0] for i in range(1, m): dp[i][0] = dp[i-1][0] + grid[i][0] for j in range(1, n): dp[0][j] = dp[0][j-1] + grid[0][j] for i in range(1, m): for j in range(1, n): dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + grid[i][j] return dp[m-1][n-1] grid = [ [1,-3,1], [1,5,-1], [4,2,1] ] print(min_path_sum(grid))
Attempts:
2 left
💡 Hint
Negative values can reduce the path sum. Check carefully the path sums.
✗ Incorrect
The minimum path sum is 2 by following the path 1 -> -3 -> 1 -> -1 -> 1.
🔧 Debug
advancedIdentify the Error in Minimum Path Sum Code
What error does the following code produce when run?
DSA Python
def min_path_sum(grid): m, n = len(grid), len(grid[0]) dp = [[0]*m for _ in range(n)] dp[0][0] = grid[0][0] for i in range(1, m): dp[i][0] = dp[i-1][0] + grid[i][0] for j in range(1, n): dp[0][j] = dp[0][j-1] + grid[0][j] for i in range(1, m): for j in range(1, n): dp[i][j] = min(dp[i-1][j], dp[i][j-1]) + grid[i][j] return dp[m-1][n-1] grid = [ [1,3,1], [1,5,1], [4,2,1] ] print(min_path_sum(grid))
Attempts:
2 left
💡 Hint
Check how dp is initialized compared to grid dimensions.
✗ Incorrect
dp is initialized with dimensions swapped (n rows, m columns), causing index errors when accessing dp[i][j].
🧠 Conceptual
advancedMinimum Path Sum with Obstacles
Given a grid where some cells are blocked (represented by -1), which approach correctly calculates the minimum path sum avoiding blocked cells?
Attempts:
2 left
💡 Hint
Blocked cells cannot be part of the path, so their dp values must reflect that.
✗ Incorrect
Setting dp values of blocked cells to infinity prevents paths through them, allowing correct minimum path sum calculation.
🚀 Application
expertMinimum Path Sum with Diagonal Moves Allowed
How would you modify the minimum path sum algorithm to allow moves right, down, and diagonally down-right? What is the minimum path sum for the grid below?
DSA Python
def min_path_sum_diag(grid): m, n = len(grid), len(grid[0]) dp = [[0]*n for _ in range(m)] dp[0][0] = grid[0][0] for i in range(1, m): dp[i][0] = dp[i-1][0] + grid[i][0] for j in range(1, n): dp[0][j] = dp[0][j-1] + grid[0][j] for i in range(1, m): for j in range(1, n): dp[i][j] = min(dp[i-1][j], dp[i][j-1], dp[i-1][j-1]) + grid[i][j] return dp[m-1][n-1] grid = [ [1,3,1], [1,5,1], [4,2,1] ] print(min_path_sum_diag(grid))
Attempts:
2 left
💡 Hint
Consider the diagonal move as an additional option in the dp calculation.
✗ Incorrect
Allowing diagonal moves reduces the minimum path sum to 5 by path 1 -> 3 -> 1 -> 1.
