Count Distinct Ways to Reach the Nth Stairs

Consider a scenario where you have been given a number of stairs. You start at the 0th stair and need to reach the Nth stair. With each stride, you can choose to either ascend one step or two steps. Your task is to determine the number of distinct ways to move from the 0th to the Nth stair.

Input:

N = 3
Output:
3
Example:

The example scenarios could be:
- Climbing one step at a time: (0, 1), (1, 2), (2, 3)
- Climbing first two steps then one step: (0, 2), (2, 3)
- Climbing one step then two steps: (0, 1), (1, 3)

Constraints:

  • 1 <= T <= 100
  • 0 <= N <= 1018
Note:

You do not need to print anything; input and output handling are already managed. Simply implement the required logic in the function.

AnswerBot
4mo

The task is to find the number of distinct ways to move from the 0th to the Nth stair by ascending one or two steps at a time.

  • Use dynamic programming to solve this problem efficiently.

  • The number of wa...read more

Sharan. Medamoni
5mo
public class Staircase { // Function to count the number of ways to reach the nth stair public static int countWays(int n) { if (n &lt;= 1) { return 1; } // Array to store the number of ways to reach ...read more
Help your peers!
Select
Add answer anonymously...

NCR Corporation Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Longest Palindromic Substring Problem Statement You are provided with a string S...read more
A Software Developer Intern was asked Q. Count Distinct Ways to Reach the Nth Stairs Consider a scenario where you have b...read more
A Software Developer Intern was asked Q. Can you describe your projects and the tech stack you used in them?

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Longest Palindromic Substring Problem Statement You are provided with a string S...read more
A Software Developer Intern was asked Q2. Count Distinct Ways to Reach the Nth Stairs Consider a scenario where you have b...read more
A Software Developer Intern was asked Q3. Can you describe your projects and the tech stack you used in them?
NCR Corporation Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits