Coffee Quality Comparison Probability
Arrays & HashingAuto-graded · 15 test casesLast updated
Calculate the probability that one person's coffee selections outperform another's under different definitions of "better"
Two people, Alice and Bob, each purchase two cups of coffee from the same shop. Each cup has a certain quality level. You need to determine the probability that Alice's coffee selections are superior to Bob's, given a specific interpretation of what "superior" means.
Assume that:
- Each cup's quality is drawn from a continuous distribution
- All four cups are independent and identically distributed
- Since the distribution is continuous, the probability of ties is zero
Your task is to compute the probability under three different interpretations of "Alice's coffees are better than Bob's":
-
"all_cross": Every one of Alice's cups must have higher quality than every one of Bob's cups (all pairwise comparisons favor Alice).
-
"rankwise": When each person's two cups are sorted by quality, Alice's better cup must beat Bob's better cup AND Alice's worse cup must beat Bob's worse cup.
-
"total": The sum of Alice's two cup qualities must exceed the sum of Bob's two cup qualities.
Constraints
- Quality values are continuous random variables (no ties)
- All four cups are independent and identically distributed
- The interpretation parameter will be one of:
"all_cross","rankwise", or"total" - Return the probability as a float
Examples
Example 1
Input: coffee_quality_probability('all_cross')
Output: 0.16666666666666666
Example 2
Input: coffee_quality_probability('rankwise')
Output: 0.3333333333333333
Example 3
Input: coffee_quality_probability('total')
Output: 0.5
Hints
Hint 1
Think about the symmetry between Alice and Bob - they both sample from the same process
Hint 2
Consider all possible rankings of the four cups and count favorable outcomes
Hint 3
The continuous distribution means you can work with ordinal rankings rather than actual values
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The key insight is to exploit symmetry. Since all four cups are drawn independently from the same continuous distribution, we can reason about their relative ranking rather than their absolute values.
With four independent continuous random variables, all 4! = 24 possible orderings are equally likely. We can also think of it as: there are C(4,2) = 6 ways to choose which 2 cups belong to Alice, and by symmetry each assignment is equally likely.
Interpretation 1: "all_cross"
Alice needs both cups to beat both of Bob's cups. This means Alice must have the two highest-quality cups overall.
Out of 6 equally likely ways to assign 2 cups to Alice from 4 total cups, only 1 assignment gives Alice both top cups.
Probability = 1/6 ≈ 0.167
Interpretation 2: "rankwise"
After sorting each person's pair, Alice's maximum must exceed Bob's maximum AND Alice's minimum must exceed Bob's minimum.
This is equivalent to requiring that Alice's worse cup still beats Bob's better cup, which means both of Alice's cups must be in the top 3 positions, with Bob's best cup at position 3 or 4, and Bob's worst cup at position 4.
Actually, Alice needs both cups strictly better than at least one of Bob's cups. Counting carefully: Alice can have positions {1,2}, {1,3}, or {2,3}. But {2,3} means Bob has {1,4}, so Bob's max at position 1 beats Alice's max at position 2 - that fails. Only {1,2} and {1,3} work, giving 2/6...
Wait, let's reconsider by direct enumeration. There are 24 orderings of 4 cups. We need A_max > B_max and A_min > B_min. This is satisfied when Alice's cups are ranked higher at both sorted positions, which happens in exactly 8 of the 24 orderings.
Probability = 8/24 = 1/3 ≈ 0.333
Interpretation 3: "total"
Alice needs sum(Alice's qualities) > sum(Bob's qualities).
By symmetry between Alice and Bob, P(Alice wins) = P(Bob wins). Since the distribution is continuous, ties have probability zero. Therefore P(Alice wins) + P(Bob wins) = 1, giving P(Alice wins) = 1/2.
Probability = 1/2 = 0.5
Complexity
- Time: O(1) - direct calculation
- Space: O(1) - no additional storage needed
Solution Code
def coffee_quality_probability(interpretation: str) -> float:
# Since all four cups are i.i.d. continuous, all 4! = 24 orderings are equally likely.
if interpretation == "all_cross":
# Alice must hold both top positions (ranks 1 and 2).
# There are C(4,2) = 6 ways to choose which 2 cups are Alice's.
# Only 1 of those has Alice holding both top spots.
return 1.0 / 6.0
if interpretation == "rankwise":
# After sorting each pair, we need A_max > B_max and A_min > B_min.
# Equivalently, the top 3 cups must include both of Alice's cups.
# There are C(4,2) = 6 ways to place Alice's cups among 4 positions.
# Favorable: Alice gets positions {1,2}, {1,3}, or {2,3} = 3 ways.
# But only {1,2} and {1,3} satisfy our condition.
# Actually, let's count directly: Alice needs at least ranks 2 and 3.
# Favorable arrangements: {1,2}, {1,3}, {2,3} but we need A_min > B_max.
# So Alice must occupy at least 2 of the top 3 positions,
# but more precisely: both Alice's cups must be better than at least one of Bob's.
# Correct count: 8 out of 24 orderings work, giving 1/3.
return 1.0 / 3.0
if interpretation == "total":
# By symmetry, P(sum_A > sum_B) = P(sum_B > sum_A).
# Since continuous distribution makes P(sum_A = sum_B) = 0,
# we have 2 * P(sum_A > sum_B) = 1.
return 0.5
raise ValueError("interpretation must be 'all_cross', 'rankwise', or 'total'")