KatInterview

Coffee Quality Comparison Probability

Arrays & HashingAuto-graded · 15 test cases
MediumAuto-gradedArrays & Hashingprobabilitycombinatoricssymmetry

Last 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":

  1. "all_cross": Every one of Alice's cups must have higher quality than every one of Bob's cups (all pairwise comparisons favor Alice).

  2. "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.

  3. "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