Data Structures & AlgorithmsHardProduct Company SDE · DP round

Problem: Climbing stairs 0 to 5 with steps of 1 or 2, but stair 3 is broken and cannot be stood on. How many ways are there to reach stair 5 from stair 0? Run the DP.

Problem: Climbing stairs 0 to 5 with steps of 1 or 2, but stair 3 is broken and cannot be stood on. How many ways are there to reach stair 5 from stair 0? Run the DP.
dp = [0] * 6
dp[0] = 1
for i in range(1, 6):
    if i == 3:
        continue
    dp[i] = dp[i - 1] + (dp[i - 2] if i >= 2 else 0)
print(dp[5])
What is printed?
  1. A. 2
  2. B. 3
  3. C. 1
  4. D. 4

Correct answer: A. 2

Stair 3 is never stood on, so the valid routes are 0-1-2-4-5 and 0-2-4-5, giving 2.

More Coding Interview Problems questions

Exams that ask this topic