Data Structures & AlgorithmsModerateTCS NQT · DP warm-up
Problem: How many ways tile a 1 x 4 strip with 1 x 1 squares and 1 x 2 dominoes? Run the code.
Problem: How many ways tile a 1 x 4 strip with 1 x 1 squares and 1 x 2 dominoes? Run the code.
f = [1, 1]
for _ in range(3):
f.append(f[-1] + f[-2])
print(f[4])What is printed?- A. 4
- B. 6
- C. 8
- D. 5
Correct answer: D. 5
The tilings satisfy t(n) = t(n-1) + t(n-2) with t(0) = t(1) = 1, giving 1, 1, 2, 3, 5. The answer is t(4) = 5.
More Coding Interview Problems questions
- Problem: How many trailing zero bits does 40 have in binary? Run the code. python print((40 & -40).bitlength() - 1) What is printed?
- Problem: How many pairs (i < j) of [2, 4, 6, 8] have gcd exactly 2? Run the code. python from itertools import combinations from math…
- Problem: Starting at (0,0) in the grid below, how many cells are reachable by 4-directional moves through '.' cells? Run the BFS. python…
- Problem: What is the minimum number of coins from {1, 5, 10, 25} summing to 63? Run the DP. python INF = 10 9 d = [0] + [INF] 63 for v in…
- 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…
- Problem: Is 'cdeab' a rotation of 'abcde'? Check with the substring test below. What is printed? python s, t = 'abcde', 'cdeab'…