DSA interview question
You're building a step-counter mini-game for a fitness app. A user is climbing a staircase with n steps, and on each move they can climb either 1 step or 2 steps at a time. To gamify the experience, the app wants to show the user a fun stat: how many distinct ways they could have climbed to the top, given their choice of 1-or-2-step moves at each point. Your task: given n, compute the total number of distinct ways to reach the top.
Example: n = 3 → ways are [1,1,1], [1,2], [2,1] → 3 distinct ways.
int climbStairs(int n);
Input n | Output | Explanation |
|---|---|---|
| 2 | 2 | Ways: [1,1], [2] |
| 3 | 3 | Ways: [1,1,1], [1,2], [2,1] |
| 1 | 1 | Only one way: [1] |
| 0 | 1 | Trivially one way: take no steps (empty sequence) |
| 4 | 5 | Ways: [1,1,1,1],[1,1,2],[1,2,1],[2,1,1],[2,2] |
| 5 | 8 | Fibonacci-like growth: 8 distinct ways |
| 10 | 89 | Matches Fibonacci sequence pattern |
(Confirm: n is non-negative; n=0 is typically defined as having exactly 1 way — the "do nothing" path — but always verify with the interviewer.)
n = 0? (Convention: 1, representing the single "already there" way — confirm this isn't ambiguous in their expected test cases.)n guaranteed non-negative? What's the expected upper bound — does it suggest recursion depth or overflow concerns (Fibonacci numbers grow exponentially)?On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in