Expected Payoff from the Higher of Two Rolls
Updated
Problem statement
A game uses a fair die with n faces numbered 1 to n. Each face comes up with probability 1/n. You roll the die twice, and the two rolls are independent.
If the two rolls show different numbers, you win the larger of the two numbers in dollars. If they show the same number, you win nothing.
Return the expected amount you win. Your answer is accepted if its absolute error or its relative error is at most 10^-9.
Function
expectedHigherRollPayoff(n: int) → doubleExamples
Example 1
n = 6return = 3.888888888888889This is the interview case. There are 36 equally likely pairs of rolls. The 6 pairs where both rolls match pay nothing. The higher roll is k in exactly 2(k - 1) of the other pairs. For example, the higher roll is 4 in (1,4), (2,4), (3,4) and the same three pairs in reverse order.
Adding pairs times payoff gives 2 × 2 + 4 × 3 + 6 × 4 + 8 × 5 + 10 × 6 = 140. The answer is 140 / 36 = 35/9, about 3.8889.
Example 2
n = 2return = 1.0There are 4 equally likely pairs. (1,1) and (2,2) match, so they pay nothing. (1,2) and (2,1) each pay 2. The answer is (2 + 2) / 4 = 1.
Example 3
n = 3return = 1.7777777777777777There are 9 equally likely pairs, and the 3 matching pairs pay nothing. (1,2) and (2,1) pay 2 each. (1,3), (3,1), (2,3) and (3,2) pay 3 each. The total is 4 + 12 = 16, so the answer is 16/9, about 1.7778.
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
- Higher Roll, or Lose the SumOA · Seen Nov 2026
- Is a Popular Story Fake?OA · 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