Chance a Pair Tops Another Pair in Four Random Ratings
Math & Bit ManipulationAuto-graded · 13 test casesLast updated
Compute the probability that two items from one group both beat two items from another group when all four scores are i.i.d. and continuous, using a symmetry and ranking-count argument.
Two Bakeries, Four Ratings
You buy two pastries from bakery X and two pastries from bakery Y. Every pastry is given a single numeric rating.
Assume the following:
- The four ratings are independent of each other.
- All four ratings come from the same continuous distribution, so neither bakery is better than the other.
- Because the distribution is continuous, ties never happen (they have probability zero). "Higher" therefore means strictly higher.
Write a function that returns the probability that both pastries from bakery X have higher ratings than both pastries from bakery Y. In other words, the smaller of the two X ratings is greater than the larger of the two Y ratings.
The function takes no arguments and returns a single float. Return the exact probability as a floating-point number (for example, the value of the fraction written as a division of two integers using floating-point arithmetic).
Constraints
- Exactly 2 pastries from each of the 2 bakeries (4 ratings in total).
- Ratings are i.i.d. and continuous; ties have probability 0.
- The function takes no input and returns a
float. - Do not approximate by simulation; return the exact value.
Examples
Example 1
Input: prob_pair_beats_pair()
Output: 0.16666666666666666
Hints
Hint 1
Because the four ratings are i.i.d. and continuous, every one of the 4! orderings of the four pastries from best to worst is equally likely.
Hint 2
The event only cares about which two pastries occupy the top two positions. Count how many of the possible 2-element subsets of pastries could be the top two, and how many of those are exactly the X pastries.
Hint 3
Check by counting full orderings: 2 ways to order the X pair, times 2 ways to order the Y pair, out of 24 total orderings. Multiplying per-pair probabilities like (1/2)*(1/2) is wrong because the comparisons are not independent.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Since the four ratings are i.i.d. and continuous, ties occur with probability zero and the ordering of the four pastries from best to worst is a uniformly random permutation. All 4! = 24 orderings are equally likely.
The event "both X pastries beat both Y pastries" holds exactly when the two X pastries occupy the top two positions in the ordering. That means the set of the top two pastries must equal the X pair.
Why 1/6
Subset counting: The set of the top two pastries is equally likely to be any of the C(4, 2) = 6 two-element subsets of the four pastries, by symmetry. Only one of those subsets is the X pair, so the probability is 1/6.
Ranking counting (check): Favorable orderings put the X pastries in positions 1 and 2 (2 orders) and the Y pastries in positions 3 and 4 (2 orders), giving 2 * 2 = 4 favorable orderings out of 24. 4 / 24 = 1/6.
Common mistake: Multiplying pairwise probabilities, such as (1/2)^4 = 1/16 for four comparisons, or (1/2)*(1/2) = 1/4, is wrong because the comparisons share pastries and are not independent.
Complexity
The answer is a constant, so time and space are both O(1).
Reference solution
def prob_pair_beats_pair() -> float:
# Four i.i.d. continuous ratings: every set of two pastries is equally
# likely to occupy the top two ranks. There are C(4, 2) = 6 such sets,
# and exactly one of them is the X pair.
return 1.0 / 6.0