KatInterview

Chance a Pair Tops Another Pair in Four Random Ratings

Math & Bit ManipulationAuto-graded · 13 test cases
EasyAuto-gradedMath & Bit Manipulationprobabilitycombinatoricssymmetry

Last 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.