Probability That One Fair Die Beats Another
ByteDanceData Scientist · Technical ScreenLast updated
Solve a coding interview problem about the probability that one fair die beats another when the dice may have different sizes and duplicate face values. The challenge emphasizes strict tie handling and an efficient approach that avoids comparing every possible pair of faces.
Probability That One Fair Die Beats Another
Two fair dice, A and B, are represented by integer arrays. Each array element is one physical face, so duplicate values are allowed and count as distinct, equally likely outcomes. The dice may have different numbers of faces.
Implement:
def solution(a: list[int], b: list[int]) -> float:
Return the probability, as a floating-point value, that a single roll of A is strictly greater than a single roll of B. Ties do not count.
Examples:
a = [1, 2, 3],b = [1, 2, 3]returns3 / 9.a = [2, 2],b = [1, 2]returns2 / 4.
Assumptions:
- Both arrays are non-empty.
- Face values are integers.
- Inputs may be large.
- Your solution should avoid enumerating all
len(a) * len(b)outcome pairs.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.