Data Structures & AlgorithmsModerateTCS NQT · DP warm-up
Problem: How many ways can a staircase of 8 steps be climbed if each move climbs 1, 3 or 4 steps? Run the DP below.
Problem: How many ways can a staircase of 8 steps be climbed if each move climbs 1, 3 or 4 steps? Run the DP below.
ways = [0] * 9
ways[0] = 1
for i in range(1, 9):
for s in [1, 3, 4]:
if i >= s:
ways[i] += ways[i - s]
print(ways[8])What is printed?- A. 25
- B. 21
- C. 29
- D. 34
Correct answer: A. 25
ways[i] = ways[i-1] + ways[i-3] + ways[i-4] with ways[0] = 1 gives 1, 1, 1, 2, 4, 6, 9, 15, 25. Treating the moves as unordered sets instead of sequences is a different count.
More Coding Interview Problems questions
- Problem: Jobs are given as (deadline, profit): (2,100), (1,19), (2,27), (1,25), (3,15). Each job takes one unit slot and must finish by…
- Problem: Compute C(10, 3) mod 7 using Lucas' theorem, where 7 is prime. What is the result?
- Problem: What is 3^100 mod 10?
- Problem: How many positive divisors does 2^4 3^2 5 7 have?
- Problem: Minimum insertions of brackets to make the string '(()))(' valid, found by this greedy scan? python s = '(()))(' bal = 0 ins = 0…
- Problem: How many contiguous subarrays of [1, 2, 1, 2, 1, 3, 1] contain exactly 2 odd numbers? python a = [1, 2, 1, 2, 1, 3, 1] k = 2 cnt…