Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice
Problem: How many 4-letter words over {x, y, z} contain at least one letter from each of the three symbols? Run the code.
Problem: How many 4-letter words over {x, y, z} contain at least one letter from each of the three symbols? Run the code.
from itertools import product
print(sum(1 for t in product('xyz', repeat=4) if len(set(t)) == 3))What is printed?- A. 81
- B. 36
- C. 33
- D. 78
Correct answer: B. 36
Inclusion-exclusion: 3⁴ − 3 · 2⁴ + 3 · 1⁴ = 81 − 48 + 3 = 36 (= 3! · S(4,3)). 81 counts all words, skipping the + 3 gives 33, and 78 counts words with at least two letters.
More Coding Interview Problems questions
- Problem: What is the number of integers x in [1, 200] for which x^2 + 2x + 1 is divisible by 7? Run the code. python print(sum(1 for x in…
- Problem: How many integer solutions (x, y) satisfy x^2 + y^2 = 65 with x, y >= 0? Run the code. python print(sum(1 for x in range(0, 9)…
- Problem: What is the maximum length of a strictly decreasing subsequence of [9, 4, 6, 3, 8, 1] ? Run the code. python a = [9, 4, 6, 3, 8…
- Problem: How many numbers in [1, 1000] are divisible by 3 and whose digit sum is also divisible by 3? Run the code. python print(sum(1 for…
- Problem: What is the time complexity of finding all prime factors of n up to 10^12 by trial division?
- Problem: Which problem is solved by the 'meet in the middle' technique on a subset-sum instance with n = 40?