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[3][3] = {{1,3,1},{1,5,1},{4,2,1}}; int m = 3, n = 3; int dp[3][3]; 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] = std::min(dp[i-1][j], dp[i][j-1]) + grid[i][j]; } } std::cout << dp[m-1][n-1] << std::endl;
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 to the bottom-right of the grid?
Attempts:
2 left
💡 Hint
Think about the problem constraints that simplify the path choices.
✗ Incorrect
The problem restricts movement to only right or down to ensure a unique path calculation.
🔧 Debug
advancedIdentify the Error in Minimum Path Sum Code
What error will the following code produce when trying to compute the minimum path sum in a grid?
DSA C++
int grid[2][2] = {{1,2},{1,1}}; int dp[2][2]; dp[0][0] = grid[0][0]; for(int i=1; i<2; i++) dp[0][i] = dp[0][i-1] + grid[0][i]; for(int j=1; j<2; j++) dp[j][0] = dp[j-1][0] + grid[j][0]; for(int i=1; i<2; i++) { for(int j=1; j<2; j++) { dp[i][j] = std::min(dp[i-1][j], dp[i][j-1]) + grid[i][j]; } } std::cout << dp[1][1] << std::endl;
Attempts:
2 left
💡 Hint
Check the initialization of dp array for the first row and column.
✗ Incorrect
The first for loop initializes dp[0][i] but uses i as column index incorrectly; similarly, dp[j][0] initialization uses j as row index but loops incorrectly causing wrong dp values.
🚀 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 paths in dp table.
✗ Incorrect
Setting dp values of blocked cells to a large number prevents paths from passing through them, effectively avoiding obstacles.
❓ Predict Output
expertOutput of Minimum Path Sum with Large Grid and Negative Values
What is the output of the following C++ code that calculates the minimum path sum in a 2x3 grid with negative values?
DSA C++
int grid[2][3] = {{1,-3,2},{-5,2,1}}; int m = 2, n = 3; int dp[2][3]; 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] = std::min(dp[i-1][j], dp[i][j-1]) + grid[i][j]; } } std::cout << dp[m-1][n-1] << std::endl;
Attempts:
2 left
💡 Hint
Trace the dp table carefully considering negative values.
✗ Incorrect
The dp table values are:
1 -2 0
-4 -2 -1
Minimum path sum is dp[1][2] = -1.
