Three-Coin Match Game
Updated
Problem statement
You flip coins fair coins at once. If they all show the same face (all heads or all tails), you win win. Otherwise you lose loss.
After you see the result, and before the game is settled, you may pick up to maxReflips of the coins and flip those again, once. You choose which coins to re-flip (possibly none) to make your expected payoff as large as possible.
Return the expected value of the game when you play this way. A positive value means the game is worth playing. Answers within 1e-6 of the exact value (relative error for values larger than 1 in absolute value) are accepted.
Function
gameValue(coins: int, maxReflips: int, win: int, loss: int) → doubleExamples
Example 1
coins = 3maxReflips = 0win = 1loss = 1return = -0.5All three coins match in 2 of the 8 equally likely results (all heads or all tails), so you win with chance 1/4. The expected value is 1/4 * 1 - 3/4 * 1 = -0.5, so the game is not worth playing.
Example 2
coins = 3maxReflips = 1win = 1loss = 1return = 0.25With chance 1/4 the coins already match, and you keep them. Otherwise two coins show one face and the third shows the other. Re-flip the odd coin: it matches the other two with chance 1/2. You win with chance 1/4 + 3/4 * 1/2 = 5/8, so the expected value is 5/8 - 3/8 = 0.25. Now the game is worth playing.
Example 3
coins = 4maxReflips = 1win = 3loss = 1return = 0.5Of the 16 results, 2 already match. In 8 of them one coin differs from the other three, and re-flipping it wins with chance 1/2. In the other 6 the split is two and two, and one re-flip cannot make all four match. You win with chance 2/16 + 8/16 * 1/2 = 3/8, so the expected value is 3/8 * 3 - 5/8 * 1 = 0.5.
Constraints
1 <= coins <= 600 <= maxReflips <= coins1 <= win, loss <= 1000
More Jane Street Phone Screen questions
- Maximum-Scoring Consecutive Tree LevelsPHONE SCREEN · Seen Nov 2026
- Tree Selection and VisibilityPHONE SCREEN · Seen Nov 2026
- Load/Store Arithmetic InterpreterPHONE SCREEN · Seen Oct 2026
- Instrument Stack Language JumpsPHONE SCREEN · Seen Oct 2026
- Run a Stack Language InterpreterPHONE SCREEN · Seen Oct 2026
- Positive-Integer Expression EvaluationPHONE SCREEN · Seen Oct 2026
- Add Two Integers Given as StringsPHONE SCREEN · Seen Sep 2026
- Angle Between Clock HandsPHONE SCREEN · Seen Jul 2026
- Route Length From Two CyclistsPHONE SCREEN · Seen Jul 2026
- Sort TradesPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
Similar questions at other companies
- Biased Coin: Probability of Exactly h HeadsAkuna Capital · PHONE SCREENAkuna Capital interview questions
- Revolver: Will the Next Shot Fire?Tower Research Capital · PHONE SCREEN
- Probability of Consecutive HeadsSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions
- Expected Fake-Name CollisionsMercor · PHONE SCREENMercor interview questions
- One Number Is the Average of the Other TwoSusquehanna International Group (SIG) · PHONE SCREENSusquehanna International Group (SIG) interview questions
- Count Routes Through a City GridIMC Trading · PHONE SCREENIMC Trading interview questions
- Extra Coin Win ProbabilityMicrosoft · PHONE SCREENMicrosoft interview questions
- Count Clean Attendance LogsRippling · PHONE SCREENRippling interview questions
- Count Array DerangementsInMobi · PHONE SCREENInMobi interview questions
- Next Pascal Row CoefficientIntuit · OAIntuit interview questions