Data Structures & AlgorithmsModerateProduct Company SDE · CP practice
Problem: How many strings of length 5 over {a, b, c} have no two adjacent equal characters? Run the code.
Problem: How many strings of length 5 over {a, b, c} have no two adjacent equal characters? Run the code.
from itertools import product
print(sum(1 for t in product('abc', repeat=5) if all(t[i] != t[i + 1] for i in range(4))))What is printed?- A. 48
- B. 243
- C. 32
- D. 24
Correct answer: A. 48
3 choices for the first letter and 2 for each of the next 4: 3 · 2⁴ = 48. 3⁵ = 243 ignores the rule, 2⁵ = 32 gives the first letter 2 choices, and 3 · 2³ = 24 misses one step.
More Coding Interview Problems questions
- Problem: How many inversions does [9, 7, 5, 3, 1, 2, 4, 6, 8] have? Run the code. python a = [9, 7, 5, 3, 1, 2, 4, 6, 8] print(sum(1 for i…
- Problem: What is the maximum number of intervals from [[1,5],[2,6],[4,8],[7,9]] that contain a common point? Run the sweep code. python iv…
- Problem: What is the minimum number of coins from {1, 7, 10} that sum to 99? Run the DP. python INF = 10 9 d = [0] + [INF] 99 for v in…
- Problem: How many palindromic numbers below 10^4 are divisible by 11? Run the code. python print(sum(1 for x in range(1, 10 4) if str(x)…
- Problem: How many integer triples (a, b, c) with 1 <= a <= b <= c <= 10 have a b c = 36? Run the code. python print(sum(1 for a in…
- Problem: How many domino tilings does a 3 x 8 board have? Run the recurrence f(n) = 4 f(n-1) - f(n-2) with f(0) = 1, f(1) = 3. python f =…