Same Number of Heads Game
Updated
Problem statement
You and the interviewer each toss n fair coins and count your heads.
- If the two head counts are equal, you pay the interviewer
equalPaydollars. - If they are different, the interviewer pays you
differentWindollars.
Return your expected gain from one game. A positive value means the game is worth playing; a negative value means you expect to lose money.
Your answer is accepted if its absolute error or its relative error is at most 10^-9.
Function
expectedGain(n: int, equalPay: int, differentWin: int) → doubleExamples
Example 1
n = 3equalPay = 2differentWin = 1return = 0.0625This is the interview game. With 3 coins, one person gets 0, 1, 2 or 3 heads with chances 1/8, 3/8, 3/8 and 1/8.
The counts are equal when both get 0, both get 1, both get 2 or both get 3: (1/8)^2 + (3/8)^2 + (3/8)^2 + (1/8)^2 = 20/64 = 5/16.
Your expected gain is 1 × 11/16 - 2 × 5/16 = 1/16 = 0.0625. It is positive, so you should play.
Example 2
n = 1equalPay = 1differentWin = 1return = 0.0With one coin each, the counts are equal half the time (both heads or both tails). You win 1 half the time and lose 1 half the time, so the expected gain is 0.
Example 3
n = 2equalPay = 3differentWin = 1return = -0.5With 2 coins, one person gets 0, 1 or 2 heads with chances 1/4, 1/2 and 1/4. The counts are equal with chance 1/16 + 1/4 + 1/16 = 3/8.
The expected gain is 1 × 5/8 - 3 × 3/8 = -4/8 = -0.5, so this game is not worth playing.
Constraints
1 <= n <= 10001 <= equalPay, differentWin <= 1000
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
- 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
- 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
- Subarray SumVisa · OAVisa interview questions