0
0
DSA Cprogramming~20 mins

Climbing Stairs Problem in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Climbing Stairs Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Climbing Stairs Recursive Function
What is the output of the following C code when n = 4?
DSA C
#include <stdio.h>

int climbStairs(int n) {
    if (n <= 2) return n;
    return climbStairs(n - 1) + climbStairs(n - 2);
}

int main() {
    int result = climbStairs(4);
    printf("%d\n", result);
    return 0;
}
A5
B7
C6
D4
Attempts:
2 left
💡 Hint
Think about how many ways to climb 3 steps and 2 steps add up for 4 steps.
Predict Output
intermediate
2:00remaining
Output of Climbing Stairs with Memoization
What is the output of this C code snippet when n = 5?
DSA C
#include <stdio.h>

int memo[100] = {0};

int climbStairs(int n) {
    if (n <= 2) return n;
    if (memo[n] != 0) return memo[n];
    memo[n] = climbStairs(n - 1) + climbStairs(n - 2);
    return memo[n];
}

int main() {
    int result = climbStairs(5);
    printf("%d\n", result);
    return 0;
}
A10
B7
C5
D8
Attempts:
2 left
💡 Hint
Memoization stores results to avoid repeated calculations.
🔧 Debug
advanced
2:30remaining
Identify the Logical Error in Climbing Stairs Iterative Code
What is the output of this code snippet and what is the logical error?
DSA C
#include <stdio.h>

int climbStairs(int n) {
    int a = 1, b = 1, c = 0;
    for (int i = 2; i <= n; i++) {
        c = a + b;
        a = b;
        b = c;
    }
    return c;
}

int main() {
    int result = climbStairs(3);
    printf("%d\n", result);
    return 0;
}
AOutput: 3; Error: Initial values cause off-by-one in counting ways
BOutput: 5; Error: Loop runs too few times
COutput: 2; Error: Variables a and b are swapped incorrectly
DOutput: 0; Error: Variable c is not initialized properly
Attempts:
2 left
💡 Hint
Check the base cases and loop start index carefully.
🧠 Conceptual
advanced
1:30remaining
Time Complexity of Climbing Stairs Recursive vs Dynamic Programming
Which option correctly describes the time complexity difference between the naive recursive and dynamic programming solutions for the climbing stairs problem?
ABoth naive recursion and dynamic programming have quadratic O(n^2) complexity
BNaive recursion is linear O(n), dynamic programming is exponential O(2^n)
CNaive recursion is exponential O(2^n), dynamic programming is linear O(n)
DNaive recursion is logarithmic O(log n), dynamic programming is linear O(n)
Attempts:
2 left
💡 Hint
Think about how many times the recursive calls repeat work.
🚀 Application
expert
3:00remaining
Number of Ways to Climb Stairs with Variable Steps
Given you can climb 1, 3, or 5 steps at a time, what is the number of ways to climb 6 steps? Choose the correct output of the following code.
DSA C
#include <stdio.h>

int climbVariableSteps(int n) {
    int dp[7] = {0};
    dp[0] = 1;
    for (int i = 1; i <= n; i++) {
        if (i - 1 >= 0) dp[i] += dp[i - 1];
        if (i - 3 >= 0) dp[i] += dp[i - 3];
        if (i - 5 >= 0) dp[i] += dp[i - 5];
    }
    return dp[n];
}

int main() {
    int result = climbVariableSteps(6);
    printf("%d\n", result);
    return 0;
}
A12
B8
C15
D10
Attempts:
2 left
💡 Hint
Count ways by adding ways from steps 1, 3, and 5 behind.