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 Java code that calculates the minimum path sum in a 3x3 grid?
DSA Java
public class Solution { public static int minPathSum(int[][] grid) { int m = grid.length; int n = grid[0].length; int[][] dp = new int[m][n]; dp[0][0] = grid[0][0]; for (int i = 1; i < m; i++) { dp[i][0] = dp[i-1][0] + grid[i][0]; } for (int j = 1; j < n; j++) { dp[0][j] = dp[0][j-1] + grid[0][j]; } for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { dp[i][j] = Math.min(dp[i-1][j], dp[i][j-1]) + grid[i][j]; } } return dp[m-1][n-1]; } public static void main(String[] args) { int[][] grid = { {1, 3, 1}, {1, 5, 1}, {4, 2, 1} }; System.out.println(minPathSum(grid)); } }
Attempts:
2 left
💡 Hint
Trace the path from top-left to bottom-right choosing the minimum sum at each step.
✗ Incorrect
The minimum path sum is calculated by dynamic programming. The path is 1 -> 3 -> 1 -> 1 -> 1 with sum 7.
🧠 Conceptual
intermediateUnderstanding Movement Constraints in Minimum Path Sum
In the Minimum Path Sum problem, which movements are allowed when moving from the top-left corner to the bottom-right corner of the grid?
Attempts:
2 left
💡 Hint
Think about the problem constraints that simplify the path choices.
✗ Incorrect
The problem restricts movement to only down or right to ensure a unique path and simplify calculation.
🔧 Debug
advancedIdentify the Error in Minimum Path Sum Code
What error will the following Java code produce when run on a non-empty grid?
DSA Java
public class Solution { public static int minPathSum(int[][] grid) { int m = grid.length; int n = grid[0].length; int[][] dp = new int[m][n]; dp[0][0] = grid[0][0]; for (int i = 1; i <= m; i++) { dp[i][0] = dp[i-1][0] + grid[i][0]; } for (int j = 1; j <= n; j++) { dp[0][j] = dp[0][j-1] + grid[0][j]; } for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { dp[i][j] = Math.min(dp[i-1][j], dp[i][j-1]) + grid[i][j]; } } return dp[m-1][n-1]; } }
Attempts:
2 left
💡 Hint
Check the loop conditions for accessing array indices.
✗ Incorrect
The loops use <= which causes index to go out of bounds since arrays are 0-indexed.
🚀 Application
advancedMinimum Path Sum with Obstacles
Given a grid where some cells are blocked (represented by -1), how would you modify the minimum path sum algorithm to avoid blocked cells?
Attempts:
2 left
💡 Hint
Think about how to represent unreachable cells in the dynamic programming table.
✗ Incorrect
Blocked cells should be treated as unreachable by assigning a very high cost so paths avoid them.
❓ Predict Output
expertOutput of Minimum Path Sum with Large Grid
What is the output of the following Java code that calculates the minimum path sum for a 4x4 grid?
DSA Java
public class Solution { public static int minPathSum(int[][] grid) { int m = grid.length; int n = grid[0].length; int[][] dp = new int[m][n]; dp[0][0] = grid[0][0]; for (int i = 1; i < m; i++) { dp[i][0] = dp[i-1][0] + grid[i][0]; } for (int j = 1; j < n; j++) { dp[0][j] = dp[0][j-1] + grid[0][j]; } for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { dp[i][j] = Math.min(dp[i-1][j], dp[i][j-1]) + grid[i][j]; } } return dp[m-1][n-1]; } public static void main(String[] args) { int[][] grid = { {1, 2, 5, 2}, {3, 2, 1, 4}, {4, 3, 2, 1}, {5, 6, 1, 1} }; System.out.println(minPathSum(grid)); } }
Attempts:
2 left
💡 Hint
Calculate the minimum sum path step by step using the dp table.
✗ Incorrect
The minimum path sum is 14 following the path with the least cumulative cost.
