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 in 3x3 Grid
What is the output of the following Rust code that calculates unique paths in a 3x3 grid?
DSA Rust
fn unique_paths(m: usize, n: usize) -> usize {
let mut dp = vec![vec![1usize; n]; m];
for i in 1..m {
for j in 1..n {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
dp[m - 1][n - 1]
}
fn main() {
let result = unique_paths(3, 3);
println!("{}", result);
}Attempts:
2 left
💡 Hint
Think about how many ways you can move right and down to reach the bottom-right corner.
✗ Incorrect
The number of unique paths in a 3x3 grid moving only right or down is 6. The dp table accumulates the sum of paths from top and left cells.
❓ Predict Output
intermediateUnique Paths Count for 2x4 Grid
What will be printed when running this Rust code that calculates unique paths in a 2x4 grid?
DSA Rust
fn unique_paths(m: usize, n: usize) -> usize {
let mut dp = vec![vec![1usize; n]; m];
for i in 1..m {
for j in 1..n {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
dp[m - 1][n - 1]
}
fn main() {
let result = unique_paths(2, 4);
println!("{}", result);
}Attempts:
2 left
💡 Hint
Count the ways to move right and down in a 2-row, 4-column grid.
✗ Incorrect
The unique paths count for a 2x4 grid is 4, calculated by summing paths from above and left cells.
🔧 Debug
advancedIdentify the Error in Unique Paths DP Initialization
What error will this Rust code produce when calculating unique paths in a grid?
DSA Rust
fn unique_paths(m: usize, n: usize) -> usize {
let mut dp = vec![vec![0usize; n]; m];
for i in 0..m {
dp[i][0] = 1usize;
}
for j in 0..n {
dp[0][j] = 1usize;
}
for i in 1..m {
for j in 1..n {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
dp[m - 1][n - 1]
}
fn main() {
let result = unique_paths(3, 3);
println!("{}", result);
}Attempts:
2 left
💡 Hint
Check the indexing of dp at the return statement.
✗ Incorrect
The code tries to access dp[m][n], but valid indices are 0 to m-1 and 0 to n-1, causing a runtime panic.
🧠 Conceptual
advancedUnderstanding DP Table Initialization in Unique Paths
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 to cells in the first row or first column.
✗ Incorrect
The first row and column cells can only be reached by moving straight right or down, so they have exactly one unique path.
❓ Predict Output
expertOutput of Unique Paths with Obstacle Grid
What is the output of this Rust code that calculates unique paths in a 3x3 grid with an obstacle at position (1,1)?
DSA Rust
fn unique_paths_with_obstacle(obstacle_grid: Vec<Vec<i32>>) -> i32 {
let m = obstacle_grid.len();
let n = obstacle_grid[0].len();
let mut dp = vec![vec![0; n]; m];
for i in 0..m {
if obstacle_grid[i][0] == 1 {
break;
}
dp[i][0] = 1;
}
for j in 0..n {
if obstacle_grid[0][j] == 1 {
break;
}
dp[0][j] = 1;
}
for i in 1..m {
for j in 1..n {
if obstacle_grid[i][j] == 1 {
dp[i][j] = 0;
} else {
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
}
}
dp[m - 1][n - 1]
}
fn main() {
let grid = vec![
vec![0, 0, 0],
vec![0, 1, 0],
vec![0, 0, 0],
];
let result = unique_paths_with_obstacle(grid);
println!("{}", result);
}Attempts:
2 left
💡 Hint
The obstacle blocks some paths; count only valid routes.
✗ Incorrect
With an obstacle at (1,1), only 2 unique paths remain to reach the bottom-right corner.
