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 code that calculates unique paths in a 3x3 grid?
DSA Python
def unique_paths(m, n): dp = [[1]*n for _ in range(m)] for i in range(1, m): for j in range(1, n): dp[i][j] = dp[i-1][j] + dp[i][j-1] return dp[m-1][n-1] print(unique_paths(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 number of unique paths in a 3x3 grid is 6 because you can only move right or down, and the total moves are fixed. The DP table sums paths from top and left cells.
🧠 Conceptual
intermediateUnderstanding DP Table Initialization
Why do we initialize the first row and first column of the DP table with 1s 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
The first row cells can only be reached by moving right from the start, and the first column cells can only be reached by moving down. So each has exactly one path.
🔧 Debug
advancedIdentify the Error in DP Update
What error will occur when running this code for unique paths in a 3x3 grid?
DSA Python
def unique_paths(m, n): dp = [[1]*n for _ in range(m)] for i in range(m): for j in range(n): if i > 0 and j > 0: dp[i][j] = dp[i-1][j] + dp[i][j-1] return dp[m-1][n-1] print(unique_paths(3, 3))
Attempts:
2 left
💡 Hint
Check the loop ranges and conditions carefully.
✗ Incorrect
The code correctly initializes dp and updates cells only when i>0 and j>0, so no invalid index is accessed. The output is correct.
❓ Predict Output
advancedOutput with Obstacles in Grid
What is the output of this code that calculates unique paths in a 3x3 grid with an obstacle at position (1,1)?
DSA Python
def unique_paths_with_obstacles(obstacle_grid): m, n = len(obstacle_grid), len(obstacle_grid[0]) dp = [[0]*n for _ in range(m)] dp[0][0] = 1 if obstacle_grid[0][0] == 0 else 0 for i in range(m): for j in range(n): if obstacle_grid[i][j] == 1: dp[i][j] = 0 else: if i > 0: dp[i][j] += dp[i-1][j] if j > 0: dp[i][j] += dp[i][j-1] return dp[m-1][n-1] obstacle_grid = [ [0,0,0], [0,1,0], [0,0,0] ] print(unique_paths_with_obstacles(obstacle_grid))
Attempts:
2 left
💡 Hint
The obstacle blocks some paths, reducing total unique paths.
✗ Incorrect
The obstacle at (1,1) blocks paths through that cell. Only two unique paths remain from start to end.
🧠 Conceptual
expertTime Complexity of Unique Paths DP
What is the time complexity of the dynamic programming solution to the Unique Paths problem for an m x n grid?
Attempts:
2 left
💡 Hint
Consider how many cells the DP table fills and how many operations per cell.
✗ Incorrect
The DP solution fills an m by n table once, doing constant work per cell, so time complexity is O(m*n).
