Amazon Interview Question: Unique Staircase Combinations
By: Angad Singh
Tags: Competitive Programming, Coding Interviews, Programming, JavaScript, Coding

Question

Given a staircase with n number of steps and provided you can only climb 1 or 2 steps at a time, write a function that returns the number of unique ways to climb the staircase.

For e.g. If n is 4, then the answer is 5 unique ways to climb. Here they are:

1, 1, 1, 1

2, 1, 1

1, 2, 1

1, 1, 2

2, 2

Answer

If we think about the problem, the number of steps needed to climb the staircase depends on the sum of the number of steps needed for its previous 2 predecessors. I.e. to calculate uniqueWays(3) = uniqueWays(2) + uniqueWays(1) . To calculate the number of unique ways you would climb a staircase with 3 steps, you can simply add uniqueWays(1) (1; since a 1 step staircase only has 1 unique way) and uniqueWays(2) (2; since a 2 step staircase has two unique ways — 1, 1 and 2 ).

So to simplify:

uniqueWays(2) === 2

uniqueWays(1) === 1

Thus: uniqueWays(3) === 2 + 1 .

This is confirmed since sum of all unique ways to climb a 3 step staircase is 3:

1, 1, 1

1, 2

2, 1

Ok, let’s confirm this theory by looking at uniqueWays(4) . From the “Question” section of the blog post we know that uniqueWays(4) === 5 . And so far we know:

uniqueWays(3) === 3;

uniqueWays(2) === 2;

Thus if we do uniqueWays(3) + uniqueWays(2) we do come up with 5, which we know to be uniqueWays(4) . So we have confirmed that we simply need the answers to the previous 2 values, sum them and we get the value for the current number. Does this remind you of something? Let me write this pattern out for you as a hint;

0, 1, 1, 2, 3, 5, 8, 13, 21

That’s right! It’s the fibonacci sequence!: f(n) = f(n — 1) + f(n — 2) .

This makes the problem super easy to solve now. We can simply use recursion to answer this question:

function uniqueWays(n) {

if (n <=1) return 1;

return uniqueWays(n - 1) + uniqueWays(n - 2);

}

This works but it is really slow. It has a time complexity of O(2^n) — we are doing a lot of repeated computations! We can do it a lot faster by just computing iteratively:

Now we have cut down the time complexity to O(n) since we are just looping through the range up to the total number of steps, n , once.

If you would like to test this out, simply open the console in your browser, paste the code from the function in it and run console.log(uniqueWays(4)) or replace 4 with any number you want the answer of.

Bonus

We have now solve the problem to a decent time complexity. How about I told you that now instead of taking only 1 or 2 steps you could take 1, 3 or 5 steps. Or 1, 2 and 3 steps at once. How can you make this function more general so it also takes a set of steps as a second argument and comes up with an answer.

Think about it, it’s not as hard as it looks. Hint: you would have to loop through the number of possible steps you can take at once too and as you compute the answers, save them in a data structure like an array where you can retrieve values based on their index. I’m looking forward to seeing your answers in the comments!

If you like questions like these, you may like a previous question such as this one.

A note from In Plain English

Did you know that we have four publications and a YouTube channel? You can find all of this from our homepage at plainenglish.io — show some love by giving our publications a follow and subscribing to our YouTube channel!