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 Javascript
const grid = [ [1,3,1], [1,5,1], [4,2,1] ]; function minPathSum(grid) { const m = grid.length; const n = grid[0].length; for (let i = 1; i < m; i++) { grid[i][0] += grid[i-1][0]; } for (let j = 1; j < n; j++) { grid[0][j] += grid[0][j-1]; } for (let i = 1; i < m; i++) { for (let j = 1; j < n; j++) { grid[i][j] += Math.min(grid[i-1][j], grid[i][j-1]); } } return grid[m-1][n-1]; } console.log(minPathSum(grid));
Attempts:
2 left
💡 Hint
Think about the path that minimizes the sum by moving only right or down.
✗ Incorrect
The minimum path sum is calculated by accumulating the smallest sums from the top-left to bottom-right. The path is 1 → 3 → 1 → 1 → 1, totaling 7.
🧠 Conceptual
intermediateUnderstanding Movement Constraints in Minimum Path Sum
In the Minimum Path Sum problem, which moves are allowed when traversing the grid?
Attempts:
2 left
💡 Hint
Consider the problem constraints about allowed directions.
✗ Incorrect
The problem restricts movement to only right or down to ensure a unique path from top-left to bottom-right.
❓ Predict Output
advancedOutput of Minimum Path Sum with Negative Values
What is the output of the code below that calculates the minimum path sum in a grid containing negative values?
DSA Javascript
const grid = [ [2, -3, 1], [5, -10, 2], [1, 2, 1] ]; function minPathSum(grid) { const m = grid.length; const n = grid[0].length; for (let i = 1; i < m; i++) { grid[i][0] += grid[i-1][0]; } for (let j = 1; j < n; j++) { grid[0][j] += grid[0][j-1]; } for (let i = 1; i < m; i++) { for (let j = 1; j < n; j++) { grid[i][j] += Math.min(grid[i-1][j], grid[i][j-1]); } } return grid[m-1][n-1]; } console.log(minPathSum(grid));
Attempts:
2 left
💡 Hint
Negative values can reduce the path sum; carefully track sums along the path.
✗ Incorrect
The minimum path sum is -8, following the path 2 → -3 → -10 → 2 → 1, totaling -8.
🔧 Debug
advancedIdentify the Error in Minimum Path Sum Code
What error does the following code produce when run?
DSA Javascript
function minPathSum(grid) {
const m = grid.length;
const n = grid[0].length;
for (let i = 1; i < m; i++) {
grid[i][0] += grid[i-1][0];
}
for (let j = 1; j < n; j++) {
grid[0][j] += grid[0][j-1];
}
for (let i = 1; i < m; i++) {
for (let j = 1; j < n; j++) {
grid[i][j] += Math.min(grid[i-1][j], grid[i][j-1]);
}
}
return grid[m-1][n-1];
}
const grid = [[1,2],[1,1]];
console.log(minPathSum(grid));Attempts:
2 left
💡 Hint
Check the loop conditions and array indexing carefully.
✗ Incorrect
The loops use <= which causes index out of bounds, leading to undefined access and TypeError.
🚀 Application
expertMinimum Path Sum with Obstacles
Given a grid where some cells are blocked (represented by -1), which modification correctly calculates the minimum path sum avoiding blocked cells? Blocked cells cannot be part of the path.
DSA Javascript
const grid = [ [1, 3, 1], [1, -1, 1], [4, 2, 1] ]; function minPathSumWithObstacles(grid) { const m = grid.length; const n = grid[0].length; const dp = Array.from({ length: m }, () => Array(n).fill(Infinity)); if (grid[0][0] === -1) return -1; dp[0][0] = grid[0][0]; for (let i = 1; i < m; i++) { if (grid[i][0] === -1) break; dp[i][0] = dp[i-1][0] + grid[i][0]; } for (let j = 1; j < n; j++) { if (grid[0][j] === -1) break; dp[0][j] = dp[0][j-1] + grid[0][j]; } for (let i = 1; i < m; i++) { for (let j = 1; j < n; j++) { if (grid[i][j] === -1) continue; dp[i][j] = Math.min(dp[i-1][j], dp[i][j-1]) + grid[i][j]; } } return dp[m-1][n-1] === Infinity ? -1 : dp[m-1][n-1]; } console.log(minPathSumWithObstacles(grid));
Attempts:
2 left
💡 Hint
Blocked cells should not be included in the path and cause path to stop if no alternative exists.
✗ Incorrect
The minimum path sum avoiding the blocked cell (-1) is 7, following the path 1 → 3 → 1 → 1 → 1.
