Red Balls Swapped for Blue
Updated
Problem statement
A bag holds n balls. r of them are red and the rest are blue.
You repeat one step: draw a ball uniformly at random from the bag, set it aside, and put one new blue ball into the bag. So the bag always holds n balls, and a red ball that has been drawn never comes back.
Return the expected number of draws until no red ball is left in the bag.
Your answer is accepted if its absolute error or its relative error is at most 10^-9.
Function
expectedDrawsToClearRed(n: int, r: int) → doubleExamples
Example 1
n = 3r = 2return = 4.5This is the interview bag: 2 red balls and 1 blue ball.
While 2 reds are left, a draw picks a red with chance 2/3, so on average it takes 3/2 draws to remove the first red. Then 1 red is left and each draw picks it with chance 1/3, which takes 3 draws on average. The answer is 3/2 + 3 = 4.5.
Example 2
n = 5r = 1return = 5.0One red ball among 5. Each draw picks it with chance 1/5, and draws that pick a blue ball change nothing. The wait for an event with chance 1/5 per try is 5 tries on average.
Example 3
n = 4r = 4return = 8.333333333333334All 4 balls start red. The waits are 4/4, 4/3, 4/2 and 4/1 as the reds go from 4 down to 1. Their sum is 1 + 4/3 + 2 + 4 = 25/3, about 8.3333.
Constraints
1 <= r <= 10^6r <= n <= 10^9
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
- Automation Pipeline, Part 1: Single WorkerPlaid · PHONE SCREENPlaid interview questions
- Subtract Two Large Number StringsStackAdapt · PHONE SCREENStackAdapt interview questions
- Circular Primes Below a LimitAmazon · PHONE SCREEN
- Palindrome NumberKotak Mahindra Bank · PHONE SCREENKotak Mahindra Bank interview questions
- Streaming AverageXantium · PHONE SCREEN
- Fair Coin from a Biased CoinAnthropic · PHONE SCREENAnthropic interview questions
- Stack Formula InterpreterTypeSafe AI · PHONE SCREEN
- Employee Count Median, Mean and ModeClay · PHONE SCREEN
- Newton Square Root to a Guaranteed ToleranceSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions
- Biased Coin: Probability of Exactly h HeadsAkuna Capital · PHONE SCREENAkuna Capital interview questions