Prepin
Log in

Determine the total ways to ascend a staircase.

Approach / Explanation

This question requires you to calculate the different ways to ascend a staircase. To clarify, for a staircase with n steps, you have the option to climb either 1 step at a time, 2 steps at a time, or possibly more, depending on the specifics given in the question. Your goal is to figure out the total number of unique ways to reach the top of the staircase. This is a well-known Dynamic Programming problem that can be approached using either a bottom-up or a top-down strategy. The essential concept is to establish that the number of ways to arrive at a particular step is the total of the ways to reach the preceding steps.

Suggested Answer

Let's assume we can ascend either 1 or 2 steps at a time. We can utilize Dynamic Programming to tackle this problem. To begin, we need to create an array ways[] with a size of n+1 to store the number of ways to reach each step. Set ways[0] and ways[1] to 1, as there is exactly one way to reach the first step and the 0th step, which is simply standing still. From the 2nd step onward, we can arrive at a specific step either from the previous step or from two steps back. Therefore, for all values of i that are greater than or equal to 2, the number of ways to reach the ith step is the sum of the ways to reach the (i-1)th step and the (i-2)th step, expressed as ways[i] = ways[i-1] + ways[i-2] . In the end, ways[n] will provide the total number of ways to reach the n-th step.

Alternative Answer

If there's a variation of the question that permits climbing up to m steps at once, the reasoning is largely the same with a slight modification. Begin by setting ways[0] to 1, and for each i from 1 to m, update ways[i] to ways[i] + ways[i-step] for each step ranging from 1 to i . From the m+1th step onward, ways[i] for all i >= m+1 will be calculated as ways[i] + ways[i-step] for step values from 1 to m . This indicates that ways[i] represents the total number of ways to reach the ith step based on the previous steps that are no more than m steps behind. Return ways[n] as the final result.

Question Details

Difficulty & Category

Medium
algorithms

Companies

Google

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.