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 C# code that calculates the minimum path sum in a 3x3 grid?
DSA C#
int[][] grid = new int[][] { new int[] {1, 3, 1}, new int[] {1, 5, 1}, new int[] {4, 2, 1} }; 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]; } } Console.WriteLine(dp[m - 1, n - 1]);
Attempts:
2 left
💡 Hint
Trace the path with the smallest sum from top-left to bottom-right, moving only right or down.
✗ Incorrect
The minimum path sum is calculated by dynamic programming. The path is 1 → 3 → 1 → 1 → 1, summing to 7.
🧠 Conceptual
intermediateUnderstanding Movement Constraints in Minimum Path Sum
In the minimum path sum problem on a grid, which movement directions are allowed to find the path?
Attempts:
2 left
💡 Hint
Think about how the problem restricts movement to avoid cycles.
✗ Incorrect
The problem restricts movement to only right or down to ensure a unique path and avoid cycles.
🔧 Debug
advancedIdentify the Error in Minimum Path Sum Code
What error will the following C# code produce when calculating minimum path sum?
DSA C#
int[][] grid = new int[][] { new int[] {1, 2}, new int[] {1, 1} }; 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];
Attempts:
2 left
💡 Hint
Check the loop boundary conditions carefully.
✗ Incorrect
The for loop uses i <= m which causes index i to go out of bounds since valid indices are 0 to m-1.
🚀 Application
advancedMinimum Path Sum in a Larger Grid
Given the grid below, what is the minimum path sum from top-left to bottom-right?
DSA C#
int[][] grid = new int[][] { new int[] {5, 9, 6}, new int[] {11, 5, 2}, new int[] {4, 7, 1} }; // Calculate minimum path sum using the standard DP approach
Attempts:
2 left
💡 Hint
Add the smallest sums step by step from top-left to bottom-right.
✗ Incorrect
The minimum path sum is 22, following the path 5 → 9 → 5 → 2 → 1.
❓ Predict Output
expertOutput of Minimum Path Sum with Negative Values
What is the output of this C# code that calculates minimum path sum in a grid containing negative values?
DSA C#
int[][] grid = new int[][] { new int[] {2, -3, 1}, new int[] {1, -5, 2}, new int[] {4, 2, -1} }; 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]; } } Console.WriteLine(dp[m - 1, n - 1]);
Attempts:
2 left
💡 Hint
Negative values can reduce the path sum; carefully track sums at each step.
✗ Incorrect
The minimum path sum is -5 following the path 2 → -3 → -5 → 2 → -1.
