Coding Interview Prep Net Eng
Coding Interview Prep Net Eng
This interview will assess the coding skills you’ll use every day.
Page 8
Additional Resources: Coding
Here is a sample coding question + solution
Climbing Stairs
Source: https://leetcode.com/problems/climbing-stairs/
You are climbing a staircase. It takes n steps to reach the top. Each time you can either climb 1 or 2 steps. In how many
distinct ways can you climb to the top?
Example 1:
Input: 2
Output: 2
Explanation: There are two ways to climb to the top
• 1 step + 1 step
• 2 steps
Example 2:
Input: 3
Output: 3
Explanation: There are 3 ways to climb to the top.
• (1) 1 step + 1 step + 1 step
• (2) 1 step + 2 step
• (3) 2 step + 1 step
Page 9
Additional Resources: Coding – cont.
Problem walkthrough
If you can climb 1 or 2 steps then you must climb to step n via step n-1 or n-2 (or both). The reason is you can’t climb 3 steps
(or more) at once.
1. Scenario #1 - assuming you are already at step n-1, there is only one option to climb to step n (climb one step).
Therefore, if there are X distinct ways to get to step n-1, and we just established there is only one option to climb to
step n, then the distinct ways to get to step n from n-1 is also X. This is idenfied in example 2 (previous slide) in way
(1) and (3).
1. Scenario #2 - assuming you are already at step (n-2), there are two options to climb to step n (climb 2 steps at once,
or climb one step and then another step). But, one of the ways (one step followed by another step) overlaps with
scenario #1 above. Therefore there is only one new distinct way not covered in scenario #1 to get to step n from step
n-2. Assuming there are Y distinct ways to get to step n-2, and we just established there is one new distinct way to
climb to step n, the new distinct ways to get to step n from n-2 is Y.
Therefore the number of distinct ways for step n are (1) the number of distinct ways from step n-1 (X) plus (2) the number of
distinct ways from step n-2 (Y) = X+Y
Page 10
Additional Resources: Coding – cont.
Bad solution
We can solve this with a recursion. We know that for n=1 there is just one distinct way.
We know that for n=2 there is also only 1 new distinct way.
The above solution is not an optimized solution, and therefore falls short of our hiring bar. Try for example running
count_steps(400) and see that it will take a while to get a response.
In particular, for every number, we will make 2 calls to count_steps, which in turn each of them would make 2 calls (4 calls
total), each of them making 2 calls (8 calls total) until n=1. This is aligned with a runtime complexity of O(2^n).
We would need to think of a way to optimize it.
Page 11
Additional Resources: Coding – cont.
Good solution #1
Good solution #2
Page 11
Additional Resources: Coding – cont.
Both approaches are good because the big O is now O(n). The space complexity for count_steps1 is worse than
count_steps2, but will be useful in case we want to run the function more than once.
You will not be asked a more complex question than the above during your interview.
Good solution #3
Candidates may observe that this is fibonacci sequence and implement their solution using the below formula (n might need
to be adjusted)
This is not a requirement to pass our bar, and solutions #1 or #2 above are more than sufficient.
Page 11