Probability Squared Maximum Beats Minimum

Hard · Software Engineering Fundamentals · Quantitative Trader · Technical Screen

Problem Statement

Suppose two real numbers X and Y are sampled independently from a continuous Uniform(0, 1) distribution. Denote their maximum by L = max(X, Y) and their minimum by S = min(X, Y).

Implement probability_larger_squared_exceeds_smaller() -> float to return the exact analytical probability that L^2 > S holds, expressed as a standard floating-point number.

Examples

Example 1

Input: (no arguments)
Call: probability_larger_squared_exceeds_smaller()
Output: 0.6666666666666666

Modeling (X, Y) on the 1 x 1 unit square [0, 1] x [0, 1], the region where X > Y and X^2 > Y has area 1/3, and by symmetry the region where Y > X and Y^2 > X also has area 1/3. Adding both disjoint halves gives 2/3 ≈ 0.6666666666666666.

Example 2

Sample trial A: (X, Y) = (0.7, 0.3) -> L = 0.7, S = 0.3 -> L^2 = 0.49 > 0.30 (event occurs)
Sample trial B: (X, Y) = (0.6, 0.5) -> L = 0.6, S = 0.5 -> L^2 = 0.36 <= 0.50 (event fails)
Sample trial C: (X, Y) = (0.25, 0.6) -> L = 0.6, S = 0.25 -> L^2 = 0.36 > 0.25 (event occurs)

Because squaring a fraction inside (0, 1) shrinks its magnitude, L^2 > S succeeds only when the smaller draw stays strictly below the square of the larger draw, which happens on 2/3 of all (X, Y) pairs in [0, 1]^2.

Constraints

  • The function accepts zero parameters and returns a single Python float.
  • Returned values are compared within relative floating-point tolerance 1e-6, though closed-form IEEE-754 precision (2 / 3) is expected.
  • Runs in O(1) time and O(1) space without relying on random sampling.

Step-by-step walkthrough

  1. Turn probability into area on the unit square

    Two real numbers X and Y are sampled independently from Uniform(0, 1). Denote their larger draw by L = max(X, Y) and their smaller draw by S = min(X, Y). Our goal is to return the exact probability P(L^2 > S) as a Python float.

    Because X and Y are drawn independently with flat density across [0, 1], the pair (X, Y) lands with uniform density inside the 1 x 1 unit square [0, 1] x [0, 1]. Since the total area of this square is 1 * 1 = 1, the probability of any event equals the geometric area of the points (x, y) inside the square that satisfy the inequality.

    Why isn't L^2 > S automatically true given L >= S? Because squaring a number strictly between 0 and 1 makes it smaller (for instance, 0.6^2 = 0.36):

    • If (X, Y) = (0.7, 0.3), then L = 0.7 and S = 0.3. Here L^2 = 0.49 > 0.3, so the event holds.
    • If (X, Y) = (0.6, 0.5), then L = 0.6 and S = 0.5. Here L^2 = 0.36 < 0.5, so the event fails because the two draws are too close together.
  2. Sanity-check first with a simulation

    Before doing any calculus, a quick Monte Carlo simulation (estimating a probability by generating many random pairs and counting hits) tells us what number our math should produce:

    import random
    
    
    def simulate_probability(trials: int = 100_000, seed: int = 2024) -> float:
        rng = random.Random(seed)
        hits = 0
        for _ in range(trials):
            x = rng.random()
            y = rng.random()
            larger = max(x, y)
            smaller = min(x, y)
            if larger * larger > smaller:
                hits += 1
        return hits / trials
    

    With 100,000 random pairs, this prints roughly 0.667. Random sampling has noise on the order of 1 / sqrt(trials) (about 0.0015 here), so simulation cannot prove an exact value—but 0.667 strongly hints the exact answer is 2/3.

  3. Split the unit square along the diagonal to remove `max` and `min`

    Integrating max(x, y) and min(x, y) directly is awkward because their formulas switch across the main diagonal y = x. So we slice the unit square along y = x into two triangles:

    • Lower-right triangle (X > Y): here L = X and S = Y, so L^2 > S simplifies to Y < X^2.
    • Upper-left triangle (Y > X): here L = Y and S = X, so L^2 > S simplifies to X < Y^2 (equivalently, Y > sqrt(X)).
    • Diagonal line (X = Y): a 1-dimensional line inside a 2-dimensional square has zero width and therefore area 0, so ties contribute 0 probability.
    y ^
    1 +-----------------------+ (1, 1)
      |  WIN:       .       . |
      |  x < y^2  .       .   |   FAIL lens along diagonal:
      |         .  FAIL .     |   x^2 <= y <= sqrt(x)
      |        .  (L^2<=S)    |   (X and Y are too close)
      |       .     .      .  |
      |      .    .      .    |
      |     .   .     .  WIN: |
      |    .  .    .    y < x^2
    0 +---.-.---.-------------+---> x
      0                       1
    

    Because X and Y are independent and identically distributed, swapping their roles reflects the square across y = x without changing any probabilities. Thus both halves have the exact same winning area:

    P(L^2 > S) = P(X > Y and X^2 > Y) + P(Y > X and Y^2 > X)
               = 2 * P(X > Y and X^2 > Y)
    
  4. Integrate one symmetric half step by step

    Let's find the area of the lower-right winning region where both Y < X and Y < X^2 hold.

    Fix a horizontal coordinate X = x anywhere in [0, 1]. For (x, y) to pass both inequalities, y must satisfy 0 <= y < min(x, x^2). Which of x and x^2 is smaller on [0, 1]?

    Start from x <= 1 and multiply both sides by x (which is >= 0, so the inequality sign does not flip):

    x <= 1   ==>   x * x <= 1 * x   ==>   x^2 <= x   for all x in [0, 1]
    

    Because x^2 <= x everywhere on [0, 1], any y that satisfies y < x^2 automatically satisfies y < x! So the condition Y < X is redundant once Y < X^2 holds. At each fixed x, the valid y values form the vertical segment [0, x^2), whose height is x^2.

    Adding up those slice heights across x from 0 to 1 is a basic power-rule integral (the antiderivative of x^2 is x^3 / 3):

    area_when_x_is_larger = integral from 0 to 1 of x^2 dx
                          = [x^3 / 3] evaluated from x = 0 to x = 1
                          = (1^3 / 3) - (0^3 / 3)
                          = 1/3
    

    Doubling this area to include the symmetric upper-left half (Y > X and X < Y^2) gives:

    P(L^2 > S) = 1/3 + 1/3 = 2/3
    

    This matches the 0.667 estimate from our simulation.

  5. Double-check without relying on symmetry

    An independent derivation catches any subtle mistake at boundaries. Fix X = x in (0, 1) and examine the entire vertical line 0 <= y <= 1 directly:

    • When 0 <= y <= x, x is larger (L = x, S = y), so we need y < x^2. Since x^2 <= x, this segment is [0, x^2), with length x^2.
    • When x < y <= 1, y is larger (L = y, S = x), so we need y^2 > x. Taking positive square roots gives y > sqrt(x). Since x <= 1 implies sqrt(x) >= x, this segment is (sqrt(x), 1], with length 1 - sqrt(x).

    The combined winning length at a fixed x is x^2 + 1 - x^(1/2). Integrating from 0 to 1 (using antiderivatives x^3 / 3, x, and (2/3) * x^(3/2)):

    P(L^2 > S) = integral from 0 to 1 of (x^2 + 1 - x^(1/2)) dx
               = [x^3 / 3 + x - (2/3) * x^(3/2)] from 0 to 1
               = 1/3 + 1 - 2/3
               = 2/3
    

    Both routes arrive at 2/3.

  6. Complete solution

    The judged function takes no arguments and returns 2/3 as a floating-point number. Writing out the two symmetric 1/3 halves keeps the geometric derivation clear in code.

    from typing import Any, Dict, List, Optional, Set, Tuple
    
    
    def probability_larger_squared_exceeds_smaller() -> float:
        # Phase 1: Model (X, Y) as a uniformly chosen point in the 1x1 unit square [0, 1] x [0, 1].
        # Because the unit square has total area 1, any event's probability equals its geometric area.
        # The diagonal line X == Y is 1-dimensional and has area 0, so we split the square into two
        # disjoint symmetric triangles: (1) X > Y (where L = X, S = Y) and (2) Y > X (where L = Y, S = X).
    
        # Phase 2: Compute the winning area in the lower-right triangle where X > Y.
        # Here L = X and S = Y, so we need BOTH Y < X and Y < X^2.
        # Crucial boundary fact: for every x in [0, 1], multiplying x <= 1 by x >= 0 gives x^2 <= x.
        # Therefore Y < x^2 automatically guarantees Y < x, so the valid vertical slice at X = x
        # is 0 <= Y < x^2 (height x^2). Integrating x^2 dx from 0 to 1 gives [x^3 / 3]_0^1 = 1/3.
        area_when_x_is_larger = 1.0 / 3.0
    
        # Phase 3: Double the area by symmetry for the upper-left triangle where Y > X.
        # Swapping X and Y leaves their independent Uniform(0, 1) joint distribution unchanged,
        # so P(Y > X and Y^2 > X) also equals 1/3.
        area_when_y_is_larger = area_when_x_is_larger
    
        total_probability = area_when_x_is_larger + area_when_y_is_larger
        return total_probability
    

    ✅ Submit now — all tests should pass.

Intuition & how it works

Imagine throwing a dart completely at random onto a 1-meter by 1-meter square dartboard so its horizontal position X and vertical position Y each follow a continuous uniform distribution on [0, 1] (every value between 0 and 1 is equally likely) and are independent (knowing X tells you nothing about Y). Together, (X, Y) has a flat joint distribution (the combined probability spread of two variables at once) over the 1 x 1 unit square, which means any event's probability is literally the area of its region inside the square. Here L = max(X, Y) and S = min(X, Y) are the order statistics of the pair (the draws sorted from smallest to largest). Because squaring a fraction between 0 and 1 shrinks it (0.5^2 = 0.25), L^2 > S holds only when the smaller coordinate is noticeably smaller than the larger one—meaning the dart avoids a narrow, lens-shaped strip hugging the diagonal X = Y.

Why do obvious shortcuts go wrong? If you drop max and min and just compute P(X^2 > Y) = 1/3, you count only the lower-right half where X happens to be the larger coordinate and miss the symmetric upper-left half where Y is larger. Conversely, assuming L >= S implies L^2 > S fails because numbers in (0, 1) get smaller when squared. Finally, a Monte Carlo simulation (estimating probability by averaging thousands of random computer trials) has random noise that shrinks slowly at rate 1 / sqrt(N), so it is great as a sanity check (~0.667) but cannot return full floating-point precision or prove the exact fraction.

Walk through three concrete dart throws (X, Y) from Example 2 to see the geometry in action:

  • (0.7, 0.3): X > Y, so L = 0.7 and S = 0.3. Since L^2 = 0.49 > 0.3, this point lies below the curve y = x^2 and succeeds.
  • (0.6, 0.5): X > Y, so L = 0.6 and S = 0.5. Since L^2 = 0.36 < 0.5, the two draws are too close; this point sits inside the failing lens between y = x^2 and y = x.
  • (0.25, 0.6): Y > X, so L = 0.6 and S = 0.25. Since L^2 = 0.36 > 0.25, this point lies to the left of x = y^2 in the upper-left triangle and succeeds.
  • Summing all slices 0 <= y < x^2 in the X > Y half gives integral_0^1 x^2 dx = 1/3, and adding the symmetric Y > X half gives 1/3 + 1/3 = 2/3.

Complexity

  • Time: O(1), because we evaluate the closed-form mathematical constant 1.0 / 3.0 + 1.0 / 3.0 in a fixed number of arithmetic operations with no loops. (By contrast, a simulation check over N random pairs takes O(N) time because it draws and tests each pair one by one.)
  • Space: O(1), because the function stores only three floating-point scalars and allocates no arrays or auxiliary data structures.

Edge cases

  • Exact ties (X = Y): the diagonal line y = x is 1-dimensional inside a 2-dimensional square and has area 0, so assigning ties to either half changes nothing.
  • Curve boundary (L^2 = S): the parabolic arcs y = x^2 and x = y^2 also have area 0 in continuous probability, so P(L^2 > S) and P(L^2 >= S) both equal 2/3.
  • Corner endpoints (0 and 1): single boundary lines like X = 0 or X = 1 have area 0, though notice that whenever L = 1 and S < 1, 1^2 > S holds.
  • Floating-point precision: compute 1.0 / 3.0 + 1.0 / 3.0 (or 2.0 / 3.0) directly as an IEEE-754 float (0.6666666666666666) rather than returning a hand-rounded literal like 0.6667, a string, or a fractions.Fraction.

Common mistakes

  • Forgetting max and min (P(X^2 > Y) = 1/3): this integrates y < x^2 only where X plays the role of the larger variable and forgets the disjoint 1/3 contribution where Y is larger and x < y^2.
  • Assuming squaring preserves L > S: squaring increases numbers above 1 (2^2 = 4 > 2) but shrinks numbers in (0, 1) (0.5^2 = 0.25 < 0.5), creating the 1/3-area failure lens S >= L^2.
  • Picking the looser upper bound on [0, 1]: in the X > Y half we need both Y < x and Y < x^2; because x^2 <= x on [0, 1], the tighter cap is x^2, whereas mistakenly integrating up to x gives 1/2 per half and 1 overall.
  • Returning a noisy Monte Carlo result: returning a random simulation average fails strict precision assertions (1e-12) and wastes CPU cycles when the exact closed form is 2/3.

Alternative approaches

  • Joint density of (S, L) directly: for two independent Uniform(0, 1) draws, (S, L) lives on the upper-left triangle 0 <= s <= l <= 1 (area 1/2) with constant joint density 2 because both (X, Y) = (s, l) and (X, Y) = (l, s) map to the same sorted pair. Integrating the failing event s in [l^2, l] gives P(S >= L^2) = integral_0^1 2 * (l - l^2) dl = 2 * (1/2 - 1/3) = 1/3, so P(L^2 > S) = 1 - 1/3 = 2/3.
  • Conditioning on X = x without symmetry: for a fixed X = x, the winning Y values are [0, x^2) (when Y <= x) plus (sqrt(x), 1] (when Y > x), and integrating x^2 + 1 - sqrt(x) over [0, 1] gives 1/3 + 1 - 2/3 = 2/3.

Follow-up questions

  • How does the probability generalize to P(L^k > S) for an arbitrary exponent k >= 1? On [0, 1], every power k >= 1 still satisfies x^k <= x, so in the X > Y half the condition Y < x^k still implies Y < x. Integrating x^k from 0 to 1 gives 1 / (k + 1) for one half, and doubling by symmetry gives 2 / (k + 1). That reproduces 2 / (1 + 1) = 1 at k = 1, 2/3 at k = 2, and 1/2 at k = 3, shrinking toward 0 as k grows because high powers push L^k closer to 0.
  • What happens if we draw n independent Uniform(0, 1) variables and compare the overall maximum squared against the overall minimum? Compute the complement P(S >= L^2), where every draw must land in [L^2, L]: there are n ways to choose which variable equals the maximum L = l, and for a fixed l each of the remaining n - 1 variables must fall inside [l^2, l] (an interval of length l - l^2), giving P(S >= L^2) = integral_0^1 n * (l - l^2)^(n - 1) dl = n * ((n - 1)!)^2 / (2n - 1)! via the Beta integral. Subtracting from 1 gives P(L^2 > S) = 1 - n * ((n - 1)!)^2 / (2n - 1)!, which rises rapidly from 2/3 (n = 2) to 9/10 (n = 3) and 34/35 (n = 4) as L approaches 1 and S approaches 0.
  • Roughly how many Monte Carlo samples are needed to estimate this probability within 0.0005 at 95% confidence? Each random pair is a coin flip with success probability p = 2/3, whose standard error (the typical size of random fluctuation around the true mean) across N trials is sqrt(p * (1 - p) / N). Setting a 95% margin of error 1.96 * sqrt((2/9) / N) <= 0.0005 requires N >= 1.96^2 * (2/9) / 0.0005^2, or roughly 3.4 million pairs—showing why closed-form calculus beats brute-force sampling for exact probabilities.

Follow-up questions

Similar questions

More Imc interview questions

All 17 Imc questions →