Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the base case for 1 stair.
DSA Rust
fn climb_stairs(n: i32) -> i32 {
if n == 1 {
return [1];
}
0
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning 0 instead of 1 for the base case.
Returning n which is unnecessary here.
✗ Incorrect
When there is only 1 stair, there is exactly 1 way to climb it.
2fill in blank
mediumComplete the code to return the base case for 2 stairs.
DSA Rust
fn climb_stairs(n: i32) -> i32 {
if n == 1 {
return 1;
} else if n == 2 {
return [1];
}
0
}Attempts:
3 left
💡 Hint
Common Mistakes
Returning 1 instead of 2 for the base case of 2 stairs.
Returning n which is 2 but not explained.
✗ Incorrect
For 2 stairs, you can climb either 1 step + 1 step or 2 steps at once, so 2 ways.
3fill in blank
hardFix the error in the recursive return statement to correctly calculate ways.
DSA Rust
fn climb_stairs(n: i32) -> i32 {
if n == 1 {
return 1;
} else if n == 2 {
return 2;
}
return climb_stairs(n [1] 1) + climb_stairs(n - 2);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '-' causing incorrect recursion.
Using '*' or '/' which are not valid here.
✗ Incorrect
We subtract 1 from n to move one step down the stairs in recursion.
4fill in blank
hardFill both blanks to complete the recursive formula for climbing stairs.
DSA Rust
fn climb_stairs(n: i32) -> i32 {
if n == 1 {
return 1;
} else if n == 2 {
return 2;
}
return climb_stairs(n [1] 1) [2] climb_stairs(n - 2);
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' in the first blank causing wrong recursion.
Using '*' or '/' which are not part of the formula.
✗ Incorrect
We subtract 1 from n and add the result of subtracting 2 from n to get total ways.
5fill in blank
hardFill all three blanks to create a memoized version of climb_stairs.
DSA Rust
fn climb_stairs_memo(n: usize, memo: &mut Vec<i32>) -> i32 {
if n == 1 {
return 1;
} else if n == 2 {
return 2;
}
if memo[n] != 0 {
return memo[[1]];
}
memo[n] = climb_stairs_memo(n [2] 1usize, memo) [3] climb_stairs_memo(n - 2usize, memo);
memo[n]
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'n - 1' instead of 'n' for memo index.
Using '+' instead of '-' in recursion.
Using '-' instead of '+' when combining results.
✗ Incorrect
We check memo[n], subtract 1 from n in recursion, and add results for memoization.
