Data Structures & AlgorithmsVery hardGoogle · Flipkart interview
Problem: Count the ways to tile a 2 × n board with 2 × 1 dominoes and L-shaped trominoes (both rotatable).
Problem: Count the ways to tile a 2 × n board with 2 × 1 dominoes and L-shaped trominoes (both rotatable).
def tilings(n):
dp = [1, 1, 2] + [0] * n
for i in range(3, n + 1):
dp[i] = 2 * dp[i-1] + dp[i-3]
return dp[n]
print(tilings(5))What is printed?- A. 8
- B. 21
- C. 24
- D. 29
Correct answer: C. 24
Sequence: 1, 1, 2, 5, 11, 24, using dp[n] = 2·dp[n−1] + dp[n−3], which comes from folding the tromino-pair cases into the recurrence. With dominoes only the count would be Fibonacci (8 for n = 5).
More Coding Interview Problems questions
- What is the time complexity of this loop? python i = n while i > 0: for j in range(i): work() # O(1) i //= 2
- Recursive quicksort can need O(n) stack depth on adversarial input. Which change GUARANTEES O(log n) extra stack space even in the worst…
- Problem: Find the shortest substring of s that contains every character of t, counting duplicates (return the leftmost one on ties)…
- Problem: Count subarrays with EXACTLY k distinct values using exactly(k) = atMost(k) − atMost(k − 1). python def atmost(a, k): cnt, l, res…
- Find the bug: the code below should return the length of the longest substring without repeating characters. python def longest(s): last…
- Problem: Count the UNIQUE triplets (as value sets) that sum to zero, using sort + two pointers with duplicate skipping. python def…