Guessing Ball Colors One by One
Updated
Problem statement
A box holds red red balls and blue blue balls. The balls are taken out one at a time, in a uniformly random order, and are not put back.
Before each ball is taken out, you guess its color. You see every ball after it comes out, so you always know how many balls of each color are left. You score 1 point for each correct guess.
Return the expected number of correct guesses when you guess as well as possible.
Your answer is accepted if its absolute error or its relative error is at most 10^-9.
Function
expectedCorrectGuesses(red: int, blue: int) → doubleExamples
Example 1
red = 2blue = 2return = 2.8333333333333335This is the interview box. With 2 of each left, any guess is right with chance 1/2. Then one color has 2 balls left and the other has 1; guess the color with 2, which is right with chance 2/3.
If that guess was right, 1 of each is left: a guess right with chance 1/2, then the last ball is known. If it was wrong (chance 1/3), both remaining balls have the same color and both guesses are right. The total is 1/2 + 2/3 + 2/3 × (1/2 + 1) + 1/3 × 2 = 17/6, about 2.8333.
Example 2
red = 1blue = 1return = 1.5The first guess is right with chance 1/2. After that you know the color of the last ball, so the second guess is always right. The answer is 1.5.
Example 3
red = 3blue = 1return = 3.25Keep guessing red while red is the larger color. As the editorial shows, guessing the larger color gives exactly 3 right guesses on the draws where the counts are not tied, whatever the order.
The counts are tied (1 of each left) only when the blue ball is one of the last two, chance 1/2, and that tie adds half a point on average. The answer is 3 + 1/2 × 1/2 = 3.25.
Constraints
0 <= red <= 10000 <= blue <= 1000red + blue >= 1
More Susquehanna International Group (SIG) Phone Screen questions
- Dice Sum That Is a Multiple of mPHONE SCREEN · Seen Nov 2026
- Find Median from Data StreamPHONE SCREEN · Seen Nov 2026
- Painting Arcs to Nearest Neighbors on a CirclePHONE SCREEN · Seen Sep 2026
- Filter Movies by Inclusive Year RangePHONE SCREEN · Seen Jul 2026
- Best Single Reroll for Five DicePHONE SCREEN · Seen Feb 2026
- Catching the Bus to the MoviePHONE SCREEN · Seen Jan 2026
- Two Dice Summing to a TargetPHONE SCREEN · Seen Jan 2026
- Median of Uniform Draws in an IntervalPHONE SCREEN · Seen Oct 2025
- One Number Is the Average of the Other TwoPHONE SCREEN · Seen Jun 2025
- Which Coin Pattern Shows Up FirstPHONE SCREEN · Seen May 2025
Similar questions at other companies
- Dice-Walk Winning ProbabilityGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- 3D Grid Diagonal Walk ProbabilityApple · PHONE SCREENApple interview questions
- Coin Game From The EndsTekion · PHONE SCREENTekion interview questions
- Starting Value with the Longest Collatz ChainZipRecruiter · PHONE SCREENZipRecruiter interview questions
- Count Array DerangementsInMobi · PHONE SCREENInMobi interview questions
- Maximum-Product Subarray with an Empty ChoiceAkuna Capital · PHONE SCREENAkuna Capital interview questions
- Probability of Consecutive HeadsSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions
- Count Staircase WaysGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- New 21 Game ProbabilityUiPath · PHONE SCREEN
- Count Stepping Numbers In RangeAmazon · PHONE SCREEN