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?- A. 2
- B. 3
- C. 1
- 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
- Problem: Is 'cdeab' a rotation of 'abcde'? Check with the substring test below. What is printed? python s, t = 'abcde', 'cdeab'…
- Problem: Find the minimum cost of a Hamiltonian tour starting and ending at city 0, with symmetric distances D =…
- Problem: How many ordered outcomes of three fair six-sided dice sum to 9? Run the code. python print(sum(1 for a in range(1, 7) for b in…
- Problem: What is the Pisano period of the Fibonacci sequence modulo 10 (the length after which (F(n), F(n+1)) mod 10 repeats (0, 1))? Run…
- Problem: How many 4-digit numbers (1000 to 9999) are divisible by 11? Run the code. python print(sum(1 for x in range(1000, 10000) if x %…
- Problem: How many integers in [1, 100] have decimal digits summing to exactly 10? Run the code. python print(sum(1 for x in range(1, 101)…