Conditional Expected Second Roll
Updated
Problem statement
You have two dice. One has sidesA sides and the other has sidesB sides. Assume each die is fair, and its faces show each whole number from 1 up to its number of sides. The two dice count as different dice even when they have the same number of sides.
Assume one die is picked at random at the start, and each die has probability 1/2 of being picked. You keep that die and roll it twice. For whichever die is picked, the two rolls are independent: the first roll does not change the chances of the second.
You see that the first roll is observedFirstRoll. Given that first roll, return the expected value (the average result) of the second roll. The first roll tells you something about which die you hold, so take it into account. The first roll is always a face that at least one die has. Do not pick a new die for the second roll.
Here the side counts and the first roll are inputs. The first example is the classic case: a 6-sided die, a 4-sided die, and a first roll of 2. Complete expectedSecondRoll(sidesA, sidesB, observedFirstRoll). It returns a double. Do not round it to an integer.
Your answer is accepted when abs(actual - expected) <= 1e-9 * max(1, abs(expected)). So the allowed error is 1e-9, measured as an absolute error for small answers and as a relative error for large ones.
Function
expectedSecondRoll(sidesA: int, sidesB: int, observedFirstRoll: int) → doubleExamples
Example 1
sidesA = 6sidesB = 4observedFirstRoll = 2return = 2.9This first roll is possible on both dice, but it is more likely on the smaller die. After seeing it, the chance of each die becomes 2/5 for the six-sided die and 3/5 for the four-sided die. Their average second rolls are 3.5 and 2.5. Weighting them by those chances gives 2.9.
Example 2
sidesA = 6sidesB = 4observedFirstRoll = 6return = 3.5Only the six-sided die has the face 6, so that must be the picked die. The answer is its average roll, 3.5.
Example 3
sidesA = 4sidesB = 6observedFirstRoll = 5return = 3.5Here the dice are listed the other way round. The first roll is a face only the six-sided die has, so the answer is that die's average roll, 3.5.
Example 4
sidesA = 5sidesB = 5observedFirstRoll = 3return = 3.0Both dice have the same number of sides but still count as different dice. Each has an average roll of 3, so the weighted average is also 3.
Constraints
2 <= sidesA, sidesB <= 10^61 <= observedFirstRoll <= max(sidesA, sidesB)- Both rolls use the same die, picked once before the first roll.
- The answer uses the updated chances of each die after you see the first roll.
More IMC Trading Phone Screen questions
- Election District Budget Allocation AnalysisPHONE SCREEN · Seen Aug 2026
- Uniform Variables: Smaller Below Larger SquaredPHONE SCREEN · Seen Mar 2026
- Combination Sum with Unlimited ReusePHONE SCREEN · Seen Dec 2025
- Valid Range of a Three-Outcome Probability ParameterPHONE SCREEN · Seen Dec 2025
- Expected Fishing CatchPHONE SCREEN · Seen Dec 2025
- Count Routes Through a City GridPHONE SCREEN · Seen Nov 2024
- Expected City Arrival and Return DaysPHONE SCREEN · Seen Nov 2024
- Stationary City Population SharesPHONE SCREEN · Seen Nov 2024
- Maximum Falling Path SumPHONE SCREEN · Seen Nov 2024
- Trading Profit Percentage ChangePHONE SCREEN · Seen Feb 2024
Similar questions at other companies
- Parking Fee CalculatorBreeze · PHONE SCREEN
- Merchant Payout Reserve CalculatorStripe · PHONE SCREENStripe interview questions
- Sqrt(x)LinkedIn · PHONE SCREENLinkedIn interview questions
- 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