Challenge - 5 Problems
Climbing Stairs Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of climbingStairs(4) with simple recursion
What is the output of the following TypeScript function call: climbingStairs(4)?
DSA Typescript
function climbingStairs(n: number): number {
if (n <= 2) return n;
return climbingStairs(n - 1) + climbingStairs(n - 2);
}
console.log(climbingStairs(4));Attempts:
2 left
💡 Hint
Think about how many ways to climb 3 steps and 2 steps add up for 4 steps.
✗ Incorrect
The function uses recursion where climbingStairs(n) = climbingStairs(n-1) + climbingStairs(n-2). For n=4, it calculates 3 + 2 = 5.
❓ Predict Output
intermediate2:00remaining
Output of climbingStairs(5) with memoization
What is the output of the following TypeScript code when calling climbingStairs(5)?
DSA Typescript
function climbingStairs(n: number, memo: Record<number, number> = {}): number {
if (n <= 2) return n;
if (memo[n] !== undefined) return memo[n];
memo[n] = climbingStairs(n - 1, memo) + climbingStairs(n - 2, memo);
return memo[n];
}
console.log(climbingStairs(5));Attempts:
2 left
💡 Hint
Use the relation climbingStairs(n) = climbingStairs(n-1) + climbingStairs(n-2) with memoization.
✗ Incorrect
climbingStairs(5) = climbingStairs(4) + climbingStairs(3) = 5 + 3 = 8. But memoization stores results to avoid repeated calls.
🔧 Debug
advanced2:00remaining
Identify the error in climbingStairs iterative solution
What error will the following TypeScript code produce when calling climbingStairs(3)?
DSA Typescript
function climbingStairs(n: number): number {
let a = 1, b = 2;
for (let i = 3; i <= n; i++) {
let c = a + b;
a = b;
b = c;
}
return b;
}
console.log(climbingStairs(3));Attempts:
2 left
💡 Hint
Check initial values and loop start index carefully.
✗ Incorrect
The function returns 3 for climbingStairs(3) because it correctly sums ways for steps 1 and 2 and iterates once for step 3.
🧠 Conceptual
advanced1:00remaining
Number of ways to climb 0 steps
According to the climbing stairs problem, how many ways are there to climb 0 steps?
Attempts:
2 left
💡 Hint
Think about the base case for no steps.
✗ Incorrect
There is exactly 1 way to climb 0 steps: by not climbing at all. This is the base case in many solutions.
🚀 Application
expert3:00remaining
Minimum number of jumps to reach the top
Given an array where each element represents the maximum jump length from that position, what is the minimum number of jumps to reach the last index? For example, for [2,3,1,1,4], what is the minimum jumps needed?
Attempts:
2 left
💡 Hint
Try to jump to the farthest reachable index at each step.
✗ Incorrect
From index 0 jump 1 step to index 1, then jump 3 steps to the last index. Total jumps = 2.