Sum of Two Uniform Numbers in a Range
Updated
Problem statement
X is a random real number chosen uniformly between 0 and a. Y is a random real number chosen uniformly between 0 and b. The two choices are independent.
Return the probability that lo <= X + Y <= hi.
Your answer is accepted if its absolute error or its relative error is at most 10^-9.
Function
sumInRangeProbability(a: int, b: int, lo: int, hi: int) → doubleExamples
Example 1
a = 1b = 2lo = 1hi = 2return = 0.5This is the interview case. Picture every pair (X, Y) as a point in a 1 by 2 rectangle, which has area 2.
For any X between 0 and 1, the good values of Y run from 1 - X to 2 - X. Both ends stay inside [0, 2], so that range always has length 1. The good area is 1 × 1 = 1, and the answer is 1 / 2 = 0.5.
Example 2
a = 1b = 1lo = 0hi = 1return = 0.5In a 1 by 1 square, X + Y <= 1 is the triangle below the diagonal, with area 1/2. So the answer is 0.5.
Example 3
a = 4b = 2lo = 3hi = 5return = 0.4375The rectangle is 4 by 2, area 8. The area where X + Y <= 5 is 7.5: the triangle with legs 5 has area 12.5, and we cut off the pieces past the sides, which have areas 0.5 and 4.5. The area where X + Y <= 3 is 4.5 - 0.5 = 4.
The answer is (7.5 - 4) / 8 = 7/16 = 0.4375.
Constraints
1 <= a, b <= 10^60 <= lo <= hi <= a + b
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
- Range of a Third CorrelationTower Research Capital · PHONE SCREEN
- Three Points in a Semicircle: Monte Carlo EstimateSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions
- Union Area of Two RectanglesDocuSign · PHONE SCREENDocuSign interview questions
- Intersection of Two RectanglesFigma · PHONE SCREENFigma interview questions
- Light Source Hits: Sample, Compare, and FitOpenAI · PHONE SCREENOpenAI interview questions
- Max Points on a LineUber · PHONE SCREENUber interview questions
- Count Axis-Aligned and Rotated SquaresGoogle · PHONE SCREENGoogle interview questions
- Best-Fit Plane Normal from 3D PointsThe Voleon Group · PHONE SCREEN
- Meeting of Points Moving Toward Initial SuccessorsThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
- Maximum Visible DronesAnduril · PHONE SCREENAnduril interview questions