Win Rate Between Two Weighted Dice
Heaps, Sorting & SearchAuto-graded · 20 test casesLast updated
Tests whether a candidate can count strict-win outcome pairs across two multi-faced dice efficiently using sorting and binary search instead of a brute-force double loop.
Win Rate Between Two Weighted Dice
You have two dice, a and b, each represented as a list of integers. Every element is one physical face of that die. Duplicate values are allowed and each duplicate counts as a separate, equally likely outcome. The two dice may have different numbers of faces.
When both dice are rolled simultaneously, what is the probability that the roll of a lands on a value strictly greater than the roll of b? A tie does not count as a win for either side.
Return that probability as a floating-point number.
Details
Every pair of faces (a[i], b[j]) is equally likely, so the answer is:
(number of pairs where a[i] > b[j]) / (len(a) * len(b))
Your solution must not enumerate all len(a) * len(b) pairs directly. For large inputs that approach is too slow; you need a more efficient strategy.
Constraints
- Both lists are non-empty.
- Face values are integers (negative, zero, or positive).
1 <= len(a), len(b) <= 10^5-10^9 <= a[i], b[j] <= 10^9- Do not deduplicate faces; each element is a distinct outcome regardless of its value.
Examples
Example 1
Input: win_probability([1, 2, 3], [1, 2, 3])
Output: 0.3333333333333333
Example 2
Input: win_probability([2, 2], [1, 2])
Output: 0.5
Hints
Hint 1
If you sort one of the lists, how quickly can you find how many of its values fall strictly below a given number?
Hint 2
bisect_left(sorted_b, x) returns the number of elements in sorted_b that are strictly less than x — which is exactly the win count for a single face x of a.
Hint 3
Keep the running win count as an integer and perform only one floating-point division at the very end to avoid accumulating rounding error.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Framing the problem
Each element of a is one face and each element of b is one face. Because every face is equally likely, the sample space is the set of all index pairs (i, j), which has size len(a) * len(b). The answer is the fraction of those pairs where a[i] > b[j].
Why the brute-force loop is ruled out
A nested loop over all pairs runs in O(n * m), which can reach 10^10 operations for the largest allowed inputs. The problem explicitly forbids this.
Efficient counting with sort + binary search
Fix one face x from a. The number of faces in b it beats is the count of elements in b that are strictly less than x. If b is sorted, that count is exactly the left insertion point of x, computed in O(log m) by bisect_left.
Algorithm:
- Sort
bintosorted_b— one-time cost ofO(m log m). - For each face
xina, addbisect_left(sorted_b, x)to an integer win counter. - Return
wins / (len(a) * len(b)).
Why it is correct
Uniform outcomes. Sorting does not merge duplicates; every element keeps its own slot. A duplicate value in b still occupies two positions, so it is counted twice as a possible losing outcome — exactly right.
Strict inequality. bisect_left returns the index of the first element >= x. Every element before that index is < x, which means it is a strict loss for b. Using bisect_right instead would include ties, overstating the win count.
No double counting. Win pairs are indexed by (i, j). Each face of a is processed once, and each binary search counts a disjoint set of j values for that specific i. Summing gives the total without overlap.
Correctness on the sample cases
a = [1, 2, 3],b = [1, 2, 3]→ sorted b is[1, 2, 3]. Wins:bisect_left(...,1)=0,bisect_left(...,2)=1,bisect_left(...,3)=2→ total3. Result:3/9 ≈ 0.333.a = [2, 2],b = [1, 2]→ sorted b is[1, 2]. Each2beats only the1→1+1=2. Result:2/4 = 0.5.
Complexity
Let n = len(a) and m = len(b).
- Time:
O(m log m)to sortb, plusO(n log m)fornbinary searches. Total:O((n + m) log m). - Space:
O(m)for the sorted copy ofb; the win counter is a single integer.
Useful follow-ups
- Two-pointer sweep: Sort both arrays, then advance a pointer through
bfor each face ofa. After the sorts, the counting pass isO(n + m), givingO(n log n + m log m)overall. Slightly faster in practice, but harder to get right quickly. - Counting-sort variant: If face values come from a small bounded range, build a prefix-sum array over that range and answer each lookup in
O(1)afterO(range)preprocessing.
Common mistakes
- Deduplicating faces — converting to sets destroys the "each face is an outcome" model and gives wrong probabilities whenever duplicates are present.
- Using
bisect_right— this counts ties as wins, producing answers that are too high. - Accumulating floating-point probabilities — adding
count / monce per face ofadrifts due to repeated float division. Integer accumulation with a single final division is exact up to the last step.
Reference solution
from bisect import bisect_left
from typing import List
def win_probability(a: List[int], b: List[int]) -> float:
# Sort b once so each face of a can be looked up with binary search.
sorted_b = sorted(b)
# bisect_left returns the insertion point before any equal value,
# so it counts exactly the faces of b that are strictly less than `face`.
wins = sum(bisect_left(sorted_b, face) for face in a)
# Accumulate wins as an exact integer; divide once to limit float error.
return wins / (len(a) * len(b))