Data Structures & AlgorithmsModerateTCS CodeVita · Microsoft SDE OA
A staircase is climbed with steps of size 1, 3 or 4 (order of steps matters). How many distinct ordered sequences of steps reach exactly height 10?
A staircase is climbed with steps of size 1, 3 or 4 (order of steps matters). How many distinct ordered sequences of steps reach exactly height 10?
n=10
dp=[0]*(n+1); dp[0]=1
for x in range(1,n+1):
for s in (1,3,4):
if x>=s: dp[x]+=dp[x-s]
print(dp[n])What is printed?- A. 28
- B. 8
- C. 64
- D. 274
Correct answer: C. 64
The ordered count is a DP over the last step: dp[x] = sum of dp[x-s]. Filling with the coin-style loop over steps outside gives unordered combinations, which is a different number, and dropping or changing a step size changes the recurrence entirely.
More Coding Interview Problems questions
- Forty-one people stand in a circle numbered 1 to 41. Starting from person 1, every third person is removed, repeatedly, counting continues…
- Compute the sum of the prefix-function (KMP failure) array of the string "abacabab", where pi[i] is the length of the longest proper…
- A directed weighted graph has edges 0→1 (2), 0→2 (2), 1→3 (1), 2→3 (1), 1→4 (4), 2→4 (5), 3→4 (3), 3→5 (4), 4→5 (1). Count the number of…
- On vertices 0..4 a weighted directed graph has these edges, stored in the order shown in the code: (3→4, −2), (2→4, 8), (4→1, 2), (1→3…
- A task graph has six nodes 0 to 5 and dependencies 5→0, 4→0, 4→1, 5→2, 2→3, 3→1 (an edge u→v means u must finish before v). Among all…
- Eight vertices 0..7 receive the edges (0,1), (2,3), (4,5), (1,2), (5,6) in that order in a disjoint-set structure with union by size and…