Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the base case for 1 stair.
DSA C++
if (n == 1) return [1];
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 0 instead of 1 for the base case.
Returning n which is unnecessary for base case.
✗ 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 C++
if (n == 2) return [1];
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 1 instead of 2 for 2 stairs.
Confusing the number of steps with the number of ways.
✗ Incorrect
For 2 stairs, you can climb either 1 step + 1 step or directly 2 steps, so 2 ways.
3fill in blank
hardFix the error in the recursive return statement to calculate ways correctly.
DSA C++
return [1](n - 1) + climbStairs(n - 2);
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect capitalization in function name.
Typos in function name causing compilation errors.
✗ Incorrect
The function name must match exactly, including case: climbStairs.
4fill in blank
hardFill both blanks to complete the memoization check and store the result.
DSA C++
if (memo[[1]] != -1) return memo[[1]]; memo[[2]] = climbStairs(n - 1) + climbStairs(n - 2);
Attempts:
3 left
💡 Hint
Common Mistakes
Using different indices for checking and storing memo results.
Using indices like n-1 or n-2 incorrectly for memo storage.
✗ Incorrect
We check and store the result for the current stair number n in memo[n].
5fill in blank
hardFill all three blanks to create a bottom-up dynamic programming solution.
DSA C++
vector<int> dp(n + 1, 0); dp[0] = 1; dp[1] = 1; for (int i = [1]; i <= n; i++) { dp[i] = dp[i [2] 1] + dp[i [3] 2]; } return dp[n];
Attempts:
3 left
💡 Hint
Common Mistakes
Starting loop from 1 instead of 2.
Using addition instead of subtraction for indices.
Using subtraction instead of addition for combining ways.
✗ Incorrect
Start loop from 2; use subtraction to access previous steps; add ways from two previous steps.
