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?- A. 504
- B. 233
- C. 927
- 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
- Problem: What is the length of the longest subarray of [1, 2, -3, 3, -1, -2] whose sum is zero? Run the prefix-sum code. python a = [1, 2…
- Problem: How many integers in [1, 100] are sums of two positive cubes? Run the code. python print(len({a 3 + b 3 for a in range(1, 5) for…
- Problem: How many distinct sums of two different elements of [1, 2, 4, 8] are there? Run the code. python from itertools import…
- Problem: How many ways are there to place 2 non-attacking rooks on a 4 x 4 board? Run the code. python from itertools import combinations…
- Problem: How many integers in [1, 1000] have exactly three distinct decimal digits? Run the code. python print(sum(1 for x in range(1…
- Problem: How many primes p <= 200 have p + 6 also prime? Run the code. python def isp(n): return n > 1 and all(n % d for d in range(2…