Data Structures & AlgorithmsModerateTCS NQT · counting
Problem: How many distinct permutations (rearrangements as strings) of 'aabc' exist? Run the code.
Problem: How many distinct permutations (rearrangements as strings) of 'aabc' exist? Run the code.
from itertools import permutations
print(len(set(permutations('aabc'))))What is printed?- A. 24
- B. 6
- C. 4
- D. 12
Correct answer: D. 12
The multinomial formula gives 4! / 2! = 12 distinct strings. Counting all 24 tuples from permutations overcounts because the two a's are identical.
More Coding Interview Problems questions
- Problem: What is the sum of C(10, k) over all even k from 0 to 10? Run the code. python from math import comb print(sum(comb(10, k) for k…
- Problem: How many integers in [100, 999] have all three digits distinct? Run the code. python print(sum(1 for x in range(100, 1000) if…
- Problem: What is the length of the longest strictly increasing contiguous run in [1, 3, 5, 4, 7, 8, 2]? Run the code. python a = [1, 3, 5…
- Problem: What is the maximum nesting depth of parentheses in '(1+(23)+((8)/4))+1'? Run the code. python s = '(1+(23)+((8)/4))+1' depth =…
- Problem: What does the Josephus recurrence J(1)=0, J(i)=(J(i-1)+2) mod i give for n = 7 (answer 1-indexed, every second person…
- Problem: How many integers in [1, 100] are divisible by both 3 and 5? Run the code. python print(sum(1 for x in range(1, 101) if x % 3 ==…