Challenge - 5 Problems
Unique Paths Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Unique Paths Calculation
What is the output of the following Go code that calculates unique paths in a 3x3 grid?
DSA Go
package main import "fmt" func uniquePaths(m int, n int) int { dp := make([][]int, m) for i := range dp { dp[i] = make([]int, n) } for i := 0; i < m; i++ { dp[i][0] = 1 } for j := 0; j < n; j++ { dp[0][j] = 1 } for i := 1; i < m; i++ { for j := 1; j < n; j++ { dp[i][j] = dp[i-1][j] + dp[i][j-1] } } return dp[m-1][n-1] } func main() { fmt.Println(uniquePaths(3, 3)) }
Attempts:
2 left
💡 Hint
Think about how many ways you can move right or down to reach the bottom-right corner.
✗ Incorrect
The code uses dynamic programming to count paths. For a 3x3 grid, the number of unique paths is 6.
🧠 Conceptual
intermediateUnderstanding DP Table Initialization
Why do we initialize the first row and first column of the DP table with 1 in the Unique Paths problem?
Attempts:
2 left
💡 Hint
Think about how you can move from the start to cells in the first row or column.
✗ Incorrect
You can only move right to reach cells in the first row and only down to reach cells in the first column, so each has exactly one path.
🔧 Debug
advancedIdentify the Bug in DP Update Loop
What error will occur when running this code snippet for unique paths in a grid?
DSA Go
func uniquePaths(m int, n int) int { dp := make([][]int, m) for i := range dp { dp[i] = make([]int, n) } for i := 0; i < m; i++ { dp[i][0] = 1 } for j := 0; j < n; j++ { dp[0][j] = 1 } for i := 1; i <= m; i++ { for j := 1; j <= n; j++ { dp[i][j] = dp[i-1][j] + dp[i][j-1] } } return dp[m-1][n-1] }
Attempts:
2 left
💡 Hint
Check the loop boundaries carefully.
✗ Incorrect
The loops use <= which causes index to go out of bounds since valid indices are 0 to m-1 and 0 to n-1.
🚀 Application
advancedNumber of Unique Paths in a 4x5 Grid
Using the unique paths DP approach, how many unique paths are there in a 4 rows by 5 columns grid?
Attempts:
2 left
💡 Hint
Use the formula or DP logic: paths = combination(m+n-2, m-1).
✗ Incorrect
Number of unique paths = C(4+5-2, 4-1) = C(7,3) = 35. But careful: 4 rows and 5 columns means m=4, n=5, so paths = C(7,3) = 35. The correct answer is 35.
❓ Predict Output
expertOutput of Unique Paths with Obstacles
What is the output of this Go code that calculates unique paths in a grid with obstacles? 0 means free cell, 1 means obstacle.
DSA Go
package main import "fmt" func uniquePathsWithObstacles(obstacleGrid [][]int) int { m := len(obstacleGrid) n := len(obstacleGrid[0]) dp := make([][]int, m) for i := range dp { dp[i] = make([]int, n) } if obstacleGrid[0][0] == 1 { return 0 } dp[0][0] = 1 for i := 1; i < m; i++ { if obstacleGrid[i][0] == 0 && dp[i-1][0] == 1 { dp[i][0] = 1 } } for j := 1; j < n; j++ { if obstacleGrid[0][j] == 0 && dp[0][j-1] == 1 { dp[0][j] = 1 } } for i := 1; i < m; i++ { for j := 1; j < n; j++ { if obstacleGrid[i][j] == 0 { dp[i][j] = dp[i-1][j] + dp[i][j-1] } } } return dp[m-1][n-1] } func main() { grid := [][]int{ {0,0,0}, {0,1,0}, {0,0,0}, } fmt.Println(uniquePathsWithObstacles(grid)) }
Attempts:
2 left
💡 Hint
Obstacles block paths. Count only valid routes avoiding obstacles.
✗ Incorrect
The obstacle at position (1,1) blocks some paths. The total unique paths from top-left to bottom-right is 2.
