Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: In how many ways can you climb 12 stairs taking 1, 2 or 3 steps at a time (order matters)? Run the DP.

Problem: In how many ways can you climb 12 stairs taking 1, 2 or 3 steps at a time (order matters)? Run the DP.
ways = [1] + [0] * 12
for v in range(1, 13):
    for s in (1, 2, 3):
        if v >= s:
            ways[v] += ways[v - s]
print(ways[12])
What is printed?
  1. A. 504
  2. B. 233
  3. C. 927
  4. D. 19

Correct answer: C. 927

ways[n] = ways[n−1] + ways[n−2] + ways[n−3] gives 927 for 12 stairs. ways[11] is 504, allowing only 1 or 2 steps gives 233, and 19 counts unordered combinations of steps.

More Coding Interview Problems questions

Exams that ask this topic