Higher Roll, or Lose the Sum
Updated
Problem statement
A fair die has n faces numbered 1 to n. You roll it twice; the two rolls are independent.
- If the two rolls are different, you win the larger number in dollars.
- If the two rolls are the same, you lose the sum of the two rolls in dollars.
Return your expected payoff. It may be negative.
Your answer is accepted if its absolute error or its relative error is at most 10^-9.
Function
expectedPayoff(n: int) → doubleExamples
Example 1
n = 6return = 2.7222222222222223This is the interview game. Of the 36 equally likely ordered pairs, 30 differ and pay their larger roll, for a total of 140. The 6 matching pairs lose 2, 4, 6, 8, 10 and 12, a total of 42.
The expected payoff is (140 - 42) / 36 = 49/18, about 2.7222.
Example 2
n = 1return = -2.0A one-faced die always shows 1 twice, so you always lose 1 + 1 = 2. The answer is -2.
Example 3
n = 2return = -0.5The pairs (1, 2) and (2, 1) each pay 2. The pair (1, 1) loses 2 and (2, 2) loses 4. The average is (2 + 2 - 2 - 4) / 4 = -0.5.
Constraints
1 <= n <= 10^6
More Susquehanna International Group (SIG) Online Assessment questions
- Catch Fish with Reusable BaitsOA · Seen Nov 2026
- Bold Play to Reach a GoalOA · Seen Nov 2026
- Cats, Truth and Who Has MoreOA · Seen Nov 2026
- Counting Cows by Their LegsOA · Seen Nov 2026
- Enough Muffins for the LineOA · Seen Nov 2026
- Expected Consecutive Pairs Among Chosen NumbersOA · Seen Nov 2026
- Four-by-Four Boundary GridOA · Seen Nov 2026
- Frog Paths Without Long RunsOA · Seen Nov 2026
- Is a Popular Story Fake?OA · Seen Nov 2026
- Knights, Knaves and Their StatementsOA · Seen Nov 2026
Similar questions at other companies
- Count Valid Square PairsAuxoAI · OA
- Server InvestmentRippling · OARippling interview questions
- Basic Item DiscountsPalantir · OAPalantir interview questions
- Identification Number Check DigitAbridge · OAAbridge interview questions
- Count Substrings With Identical CharactersVirtu Financial · OAVirtu Financial interview questions
- Next Pascal Row CoefficientIntuit · OAIntuit interview questions
- Digit-Sum Sequence Join PointOpenAI · OAOpenAI interview questions
- Minimum Testers to Find the Poisoned BottleWalleye Capital · OA
- Address on Same PageNVIDIA · OANVIDIA interview questions
- Preorder of a Special Binary TreeNielsen · OA