Data Structures & AlgorithmsHardTCS NQT · probability counting
Problem: How many ordered outcomes of three fair six-sided dice sum to 9? Run the code.
Problem: How many ordered outcomes of three fair six-sided dice sum to 9? Run the code.
print(sum(1 for a in range(1, 7) for b in range(1, 7) for c in range(1, 7) if a + b + c == 9))What is printed?
- A. 21
- B. 27
- C. 30
- D. 25
Correct answer: D. 25
The number of ordered triples summing to 9 is 25. Counting unordered multisets instead gives only 6, which is the trap.
More Coding Interview Problems questions
- Problem: What is the Pisano period of the Fibonacci sequence modulo 10 (the length after which (F(n), F(n+1)) mod 10 repeats (0, 1))? Run…
- Problem: How many 4-digit numbers (1000 to 9999) are divisible by 11? Run the code. python print(sum(1 for x in range(1000, 10000) if x %…
- Problem: How many integers in [1, 100] have decimal digits summing to exactly 10? Run the code. python print(sum(1 for x in range(1, 101)…
- Problem: What is the minimum number of bit flips needed to make the binary string '00110' non-decreasing (all 0s then all 1s)? Run the…
- Problem: How many pairs (i < j) of 'aabab' have s[i] = 'a' and s[j] = 'b'? Run the code. python s = 'aabab' print(sum(1 for i in…
- Problem: How many distinct products a[i] a[j] (i < j) does [1, 2, 3, 4] produce? Run the code. python from itertools import combinations…