Data Structures & AlgorithmsModerateProduct Company SDE · CP practice
Problem: How many three-letter words over {a, b, c} contain every letter at least once? Run the code.
Problem: How many three-letter words over {a, b, c} contain every letter at least once? Run the code.
from itertools import product
print(sum(1 for t in product('abc', repeat=3) if len(set(t)) == 3))What is printed?- A. 27
- B. 24
- C. 6
- D. 3
Correct answer: C. 6
A length-3 word using all of a, b, c is a permutation: 3! = 6. 27 counts all words, 24 counts words with at least two distinct letters, and 3 counts the letters.
More Coding Interview Problems questions
- Problem: What is the length of the longest subsequence of [3, 1, 4, 1, 5, 9, 2, 6] whose elements are strictly increasing? Run the code…
- Problem: What is the number of trailing zeros in 30!? Run the code. python n = 30 c = 0 p = 5 while p <= n: c += n // p p = 5 print(c)…
- Problem: How many integers in [1, 500] are perfect squares or multiples of 7? Run the code. python print(sum(1 for x in range(1, 501) if…
- Problem: What is the minimum number of operations to make all elements of [4, 7, 1] equal to their minimum by decrementing? Run the code…
- Problem: What is the number of paths from (1,1) to (3,3) moving only right or up in a grid with all cells free? Run the DP. python g =…
- Problem: How many distinct values does a XOR b take for a, b in {0, 1, 2, 3}? Run the code. python print(len({a ^ b for a in range(4) for…