Expected Draws to Remove All Green Counters
Updated
Problem statement
A jar contains blue blue counters and green green counters. You remove counters from the jar one by one. On each draw, every counter still inside the jar has the same chance of being the one you take, and a counter that has been taken stays out.
Drawing ends with the draw that leaves no green counter in the jar; any blue counters still inside at that moment are never drawn. Return how many draws you make on average, including that final draw.
Function
expectedDrawsUntilAllGreen(blue: int, green: int) → doubleExamples
Example 1
blue = 9green = 3return = 9.75As a fraction this is 39/4. Each of the 9 blue counters has a 3/4 chance of being drawn before the third green one, so on average 27/4 blue counters come out along with the 3 green ones.
Example 2
blue = 2green = 2return = 3.3333333333333335The 6 orders of the colors are equally likely. BBGG needs 4 draws, BGBG 4, GBBG 4, BGGB 3, GBGB 3 and GGBB 2, so the mean is 20/6 = 10/3.
Example 3
blue = 5green = 1return = 3.5Each of draws 1 to 6 takes the single green counter with chance 1/6, so the mean is 3.5.
Constraints
0 <= blue <= 10^91 <= green <= 10^9- Answers are checked with tolerance:
|output - exact| <= 10^-9 · exact, whereexactis always 1 or more.
More Optiver Online Assessment questions
- Chance a Die Game Balance Reaches ZeroOA · Seen Dec 2026
- Chance a Hand of Cards Multiplies to More Than a ThresholdOA · Seen Dec 2026
- Chance a Sum of Random Draws Has a Given Number of DigitsOA · Seen Dec 2026
- Expected Profit of a Doubling Betting SystemOA · Seen Dec 2026
- Expected Steps Until a Random Walk Gets Far From ZeroOA · Seen Dec 2026
- Rolling Until One Face's Own Total Passes a ThresholdOA · Seen Dec 2026
- Two Ranked Teams Meeting in a Random BracketOA · Seen Dec 2026
- Overheat Prevention ControllerOA · Seen Nov 2026
- Power Cell BankOA · Seen Nov 2026
- Count Straight-Line Word Occurrences in a GridOA · Seen Sep 2026
Similar questions at other companies
- Bold Play to Reach a GoalSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Maximum Score with Prime JumpsMicrosoft · OAMicrosoft interview questions
- Sliding-Window Means with IEEE Special ValuesHeadlands Technologies · OAHeadlands Technologies interview questions
- Count Distinct Arrays After Decrement OperationsBloomberg LP · OABloomberg LP interview questions
- Polynomial CoefficientsShopify · OAShopify interview questions
- Basic Calculator IITypeSafe AI · OA
- Last Object to Fall Off the TableConfluent · OAConfluent interview questions
- Last Object Exit TimeIBM · OAIBM interview questions
- Count Prime Values in SubtreesVisa · OAVisa interview questions
- Minimum Rounds to Complete All TasksInfosys · OAInfosys interview questions