One Number Is the Average of the Other Two
Updated
Problem statement
You get three numbers in one of two ways:
- If
distinctistrue, you choose three different numbers from1ton. Every set of three is equally likely. - If
distinctisfalse, you roll three fair dice with faces1ton. The rolls are independent, so numbers can repeat.
Return the probability that one of the three numbers is the average of the other two. For example, 4 is the average of 2 and 6, and 3 is the average of 3 and 3.
Your answer is accepted if its absolute error or its relative error is at most 10^-9.
Function
meanOfOtherTwoProbability(n: int, distinct: boolean) → doubleExamples
Example 1
n = 20distinct = truereturn = 0.07894736842105263This is the first interview case. One number is the average of the other two exactly when the three numbers, sorted, have equal gaps: b - d, b, b + d.
A gap d fits 20 - 2d times inside 1 to 20, for d = 1 to 9. That gives 18 + 16 + ... + 2 = 90 good sets. There are C(20, 3) = 1140 sets in all, so the answer is 90 / 1140 = 3/38, about 0.0789.
Example 2
n = 6distinct = falsereturn = 0.19444444444444445This is the second interview case: three six-sided dice, 6^3 = 216 equally likely rolls.
The 6 rolls with all three values equal work. Two equal values and one different never work. Sets with equal gaps inside 1 to 6 are 4 with gap 1 and 2 with gap 2, and each can be rolled in 6 orders, so they give 6 × 6 = 36 rolls. The answer is (6 + 36) / 216 = 7/36, about 0.1944.
Example 3
n = 5distinct = truereturn = 0.4The good sets are {1, 2, 3}, {2, 3, 4}, {3, 4, 5} and {1, 3, 5}. There are C(5, 3) = 10 sets, so the answer is 4 / 10 = 0.4.
Constraints
1 <= n <= 10^6- If
distinctistrue, thenn >= 3.
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
- Which Coin Pattern Shows Up FirstPHONE SCREEN · Seen May 2025
- Random Points Inside One ArcPHONE SCREEN · Seen Jan 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