Chance of a Red Majority When Skipping Returned Marbles
Math & Bit ManipulationAuto-graded · 22 test casesLast updated
Compute a probability for a draw process with discarded-and-returned items by reducing it to a hypergeometric count, ignoring the irrelevant returned category.
A bag holds red red marbles, blue blue marbles, and green green marbles. You pull marbles one at a time, each uniformly at random from what is currently in the bag.
- If the marble is green, you put it straight back and keep pulling.
- If the marble is red or blue, you keep it (it does not go back).
You stop as soon as you are holding exactly three marbles, all of them red or blue.
Write a function that returns the probability that at least two of the three marbles you hold are red.
Rules and edge cases
- Return the probability as a floating-point number.
- If the bag contains fewer than three non-green marbles in total (
red + blue < 3), you can never hold three marbles, so return0.0. - The value of
greenmay be any non-negative integer. Treat the process as one that ends with probability 1 wheneverred + blue >= 3.
Constraints
0 <= red, blue, green <= 1000- All inputs are integers.
- The result is compared to the expected value as a float with a small tolerance.
Examples
Example 1
Input: prob_red_majority(5, 5, 5)
Output: 0.5
Example 2
Input: prob_red_majority(10, 5, 5)
Output: 0.7582417582417582
Hints
Hint 1
Think about what a green pull changes. If the marble goes back immediately, does the bag look any different afterwards?
Hint 2
Condition on the pull being non-green: the chance it is red is red / (red + blue), regardless of how many green marbles there are. So the three kept marbles behave like three drawn without replacement from only the red and blue marbles.
Hint 3
Count favorable selections: exactly 3 red, plus exactly 2 red and 1 blue. Divide by the number of ways to choose any 3 of the red and blue marbles. Don't forget the case with fewer than 3 non-green marbles.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Green marbles go straight back into the bag, so a green pull leaves the bag unchanged and has no effect on what you end up holding. It only affects how many pulls it takes.
Look only at the pulls that are not green. When the bag has r red and b blue marbles, the probability that such a pull is red is
(r / total) / ((r + b) / total) = r / (r + b)
The green count cancels out. This is exactly the rule for drawing without replacement from a collection of only the red and blue marbles. So the three kept marbles are a uniformly random 3-subset of the red + blue non-green marbles (the process ends with probability 1 when there are at least three of them).
Counting
- Total 3-subsets:
C(red + blue, 3). - Favorable subsets (at least two red):
- exactly 3 red:
C(red, 3) - exactly 2 red and 1 blue:
C(red, 2) * blue
- exactly 3 red:
The answer is the favorable count divided by the total. If red + blue < 3, three marbles can never be held, so the answer is 0.0.
Why it is correct
Every 3-subset of the non-green marbles is equally likely as the final held set, by the symmetry of the uniform draw among non-green marbles. The two favorable cases are disjoint and together cover "at least two red" for three marbles.
Complexity
Time is dominated by computing a few binomial coefficients, which is O(1) for small inputs (polynomial in the input size in general). Space is O(1).
Reference solution
from math import comb
def prob_red_majority(red: int, blue: int, green: int) -> float:
non_green = red + blue
if non_green < 3:
return 0.0
favorable = comb(red, 3) + comb(red, 2) * blue
return favorable / comb(non_green, 3)