Upper-Tail Probability of 1,000 Die Rolls

Hard · Statistics & Math · Machine Learning Engineer · Onsite

Problem Statement

Suppose a fair six-sided die (faces 1 through 6, each occurring with probability 1/6) is tossed 1,000 times independently, and let S denote the sum of all 1,000 outcomes.

Implement probability_sum_exceeds(t: int) -> float to return the estimated probability P(S > t) that the total S is strictly greater than the integer cutoff t:

  1. Exact support checks: Because every roll lands in {1, 2, 3, 4, 5, 6}, the total S is always an integer in [1000, 6000].
    • Whenever t < 1000, S > t holds for every possible sequence of rolls, so return 1.0.
    • Whenever t >= 6000, S > t cannot occur for any sequence of rolls, so return 0.0.
  2. Continuity-corrected Gaussian approximation: For 1000 <= t < 6000, model S via the Central Limit Theorem using the exact single-roll mean E[X] = 7/2 = 3.5 and single-roll variance Var(X) = 35/12 scaled across 1,000 independent rolls (E[S] = 3500, Var(S) = 1000 * 35 / 12).
    • Because S takes integer values only, S > t is identical to S >= t + 1. Applying the half-unit continuity correction places the continuous boundary at t + 0.5, giving standardized score z = (t + 0.5 - 3500) / sqrt(1000 * 35 / 12).
    • Return the standard normal upper-tail area 1 - Phi(z), evaluated in a numerically stable form (such as 0.5 * math.erfc(z / sqrt(2))) so large positive z values do not round prematurely to 0.0 due to floating-point subtraction.

Examples

Example 1

Input: t = 900
Output: 1.0

Even if all 1,000 tosses show 1, their sum is 1,000, which strictly exceeds 900. Therefore P(S > 900) = 1.0.

Example 2

Input: t = 3581
Output: 0.065638306

S > 3581 corresponds to S >= 3582, so the continuity-corrected cutoff is 3581.5. With mean 3500 and standard deviation sqrt(1000 * 35 / 12) ≈ 54.006172, we obtain z = 81.5 / 54.006172 ≈ 1.509087 and 1 - Phi(z) ≈ 0.0656383.

Example 3

Input: t = 6200
Output: 0.0

Even rolling 6 on every single toss yields S = 6,000, so exceeding 6,200 is impossible and P(S > 6200) = 0.0.

Constraints

  • -10^9 <= t <= 10^9 (t is an integer).
  • Return a float in [0.0, 1.0] matching 1.0 for t < 1000, 0.0 for t >= 6000, and the continuity-corrected normal upper tail within 10^-6 relative/absolute tolerance for 1000 <= t < 6000.

Step-by-step walkthrough

  1. Clarify the rules and target probability

    We toss a fair six-sided die 1,000 independent times and add up all 1,000 outcomes to form S. Given an integer cutoff t, probability_sum_exceeds(t) estimates P(S > t).

    Three foundational details govern the math:

    • Strict inequality (S > t): Because S only takes whole-number values, S > t is identical to S >= t + 1 rather than S >= t.
    • Gaussian approximation: Enumerating all 6^1000 equally likely outcome sequences (a 779-digit integer) is out of reach, so we approximate S inside its interior range using the standard normal cumulative distribution function Phi.
    • Support check before standardizing: Every toss lands in {1, 2, 3, 4, 5, 6}, which locks S inside [1000, 6000]. Cutoffs below 1,000 or at/above 6,000 have exact answers 1.0 and 0.0.
  2. Moments of a single fair die roll

    Let X be the face shown on one toss, taking each value in {1, 2, 3, 4, 5, 6} with probability 1/6.

    • Expected value (E[X]): By symmetry around 3.5 (or direct averaging), E[X] = (1 + 2 + 3 + 4 + 5 + 6) / 6 = 21 / 6 = 7 / 2 = 3.5.
    • Second moment (E[X^2]): Average the six squared faces: E[X^2] = (1^2 + 2^2 + 3^2 + 4^2 + 5^2 + 6^2) / 6 = (1 + 4 + 9 + 16 + 25 + 36) / 6 = 91 / 6.
    • Variance (Var(X)): Apply Var(X) = E[X^2] - (E[X])^2 over common denominator 12: Var(X) = 91/6 - (7/2)^2 = 182/12 - 147/12 = 35/12 ≈ 2.917.
    • Standard deviation (SD(X)): Take the square root so spread is measured in die points rather than squared points: SD(X) = sqrt(35 / 12) ≈ 1.708.

    Store 35 / 12 directly in Python rather than a rounded decimal so multiplying by 1,000 stays exact.

    NUM_ROLLS = 1000
    # E[X] = (1 + 2 + 3 + 4 + 5 + 6) / 6 = 21 / 6 = 3.5
    SINGLE_ROLL_MEAN = 3.5
    # Var(X) = E[X^2] - E[X]^2 = 91/6 - 49/4 = 35/12 (keep exact fraction to avoid rounding error)
    SINGLE_ROLL_VARIANCE = 35 / 12
    
  3. Scale mean and variance across 1,000 independent rolls

    Write S = X_1 + X_2 + ... + X_1000 for the sum of all 1,000 tosses:

    • Expected sum (E[S]): By linearity of expectation, expectations add regardless of dependence: E[S] = 1000 * 3.5 = 3500.
    • Sum variance (Var(S)): Because the tosses are independent, all cross-covariances vanish and variances add: Var(S) = 1000 * (35 / 12) = 8750 / 3 ≈ 2916.67.
    • Sum standard deviation (SD(S)): Take the square root of Var(S): SD(S) = sqrt(1000 * 35 / 12) ≈ 54.006.

    Notice the scaling difference: the mean grows linearly by 1,000, whereas the standard deviation grows only by sqrt(1000) ≈ 31.62 because high and low rolls cancel each other out.

    Because S sums 1,000 independent, identically distributed bounded variables with positive variance, the Central Limit Theorem (CLT) approximates S closely by Normal(mean = 3500, variance = 8750 / 3).

  4. Apply continuity correction on the normal curve

    S is discrete (integers only), while the normal density is smooth. Picture the exact probability mass function as a histogram in which each integer k occupies a unit-width bar [k - 0.5, k + 0.5]:

    Integer outcomes:       ... |     t - 1     |       t       |     t + 1     |     t + 2     | ...
    Continuous intervals:   ... [t-1.5,  t-0.5] [t-0.5,  t+0.5] [t+0.5,  t+1.5] [t+1.5,  t+2.5] ...
                                                        ^       ^
                                               excluded |       |---> S > t means S >= t + 1,
                                               (S == t)               so integrate from t + 0.5 rightward ->
    

    Because S > t omits the bar centered at t and begins with the bar centered at t + 1, the continuous split point sits at t + 0.5:

    z_score = (t + 0.5 - 3500) / sqrt(1000 * 35 / 12)
    P(S > t) ≈ 1 - Phi(z_score)
    

    Evaluating 1.0 - Phi(z) directly in 64-bit float loses precision once z > 8.3, because Phi(z) rounds to 1.0 and 1.0 - 1.0 collapses to 0.0. Calling 0.5 * math.erfc(z / sqrt(2)) evaluates the upper tail directly without subtraction.

    import math
    
    
    def normal_upper_tail(z_score: float) -> float:
        """Return P(Z > z_score) = 1 - Phi(z_score) for a standard normal variable Z."""
        # Using erfc directly avoids 1.0 - Phi(z_score), which suffers catastrophic
        # cancellation and rounds to 0.0 as soon as Phi(z_score) rounds to 1.0 (around z > 8.3).
        return 0.5 * math.erfc(z_score / math.sqrt(2.0))
    
  5. Check physical sum bounds before standardizing

    Before computing any z-score, compare t against [1000, 6000]:

    • When t < 1000: Even a thousand consecutive 1s give S = 1000 > t, so P(S > t) = 1.0. Use strict < because at t = 1000, rolling all ones yields S = 1000, which does not strictly exceed 1000.
    • When t >= 6000: Even a thousand consecutive 6s give S = 6000, which never strictly exceeds 6000, so P(S > t) = 0.0. Use >= here.

    That settles Example 1 (t = 900 -> 1.0) and Example 3 (t = 6200 -> 0.0) immediately without touching a normal table.

    def probability_sum_exceeds(t: int) -> float:
        """Estimate P(S > t) for the sum S of 1,000 fair 6-sided die rolls."""
        # Phase 1: Check physical bounds of S, which can only take integers in [1000, 6000].
        min_sum = NUM_ROLLS * 1
        max_sum = NUM_ROLLS * 6
    
        # Strictly '<': at t = 1000, all-ones gives S = 1000, so S > 1000 is NOT 100% certain.
        if t < min_sum:
            return 1.0
    
        # '>=': S can never exceed 6000, so even at t = 6000 the event S > 6000 is impossible.
        if t >= max_sum:
            return 0.0
    
        # Phase 2: Scale mean and variance from 1 roll to 1,000 independent rolls.
        sum_mean = NUM_ROLLS * SINGLE_ROLL_MEAN           # 3500.0
        sum_variance = NUM_ROLLS * SINGLE_ROLL_VARIANCE   # 8750 / 3 ~ 2916.67 (variances add by independence)
        sum_std_dev = math.sqrt(sum_variance)             # ~ 54.01 (grows with sqrt(NUM_ROLLS))
    
        # Phase 3: Continuity correction + standardization.
        # Because S is an integer, S > t is the exact same event as S >= t + 1.
        # On a continuous curve where integer k spans [k - 0.5, k + 0.5], the bar for t + 1 starts at t + 0.5.
        cutoff = t + 0.5
        z_score = (cutoff - sum_mean) / sum_std_dev
        return normal_upper_tail(z_score)
    
  6. Trace the sample thresholds

    Let's walk through Examples 1, 2, and 3 plus the exact center t = 3500:

    1) t = 900 (Example 1):
       900 < min_sum (1000) -> returns 1.0 immediately.
    
    2) t = 3500 (right at the expected sum):
       cutoff  = 3500.5
       z_score = (3500.5 - 3500.0) / 54.006172 = +0.009258
       answer  = 1 - Phi(0.009258) ≈ 0.496307
       (Slightly under 0.5 because S == 3500 has probability ~0.0074 and does not satisfy S > 3500!)
    
    3) t = 3581 (Example 2, ~1.51 SD above the mean):
       cutoff  = 3581.5
       z_score = (3581.5 - 3500.0) / 54.006172 = +1.509087
       answer  = 1 - Phi(1.509087) ≈ 0.065638
    
    4) t = 6200 (Example 3):
       6200 >= max_sum (6000) -> returns 0.0 immediately.
    

    Symmetry provides a quick sanity check on + 0.5: mapping each toss X_i -> 7 - X_i turns S into 7000 - S, so P(S > t) + P(S > 6999 - t) must equal 1.0. With + 0.5, the two cutoffs t + 0.5 and 7000 - t - 0.5 sit at equal and opposite offsets from 3500, making the two normal tails add up to 1.0!

  7. Complete solution

    from typing import Any, Dict, List, Optional, Set, Tuple
    import math
    
    # Constants for 1,000 independent rolls of a fair 6-sided die {1, 2, 3, 4, 5, 6}.
    NUM_ROLLS = 1000
    # E[X] = (1 + 2 + 3 + 4 + 5 + 6) / 6 = 21 / 6 = 3.5
    SINGLE_ROLL_MEAN = 3.5
    # Var(X) = E[X^2] - E[X]^2 = 91/6 - 49/4 = 35/12 (keep exact fraction to avoid rounding error)
    SINGLE_ROLL_VARIANCE = 35 / 12
    
    
    def normal_upper_tail(z_score: float) -> float:
        """Return P(Z > z_score) = 1 - Phi(z_score) for a standard normal variable Z."""
        # Using erfc directly avoids 1.0 - Phi(z_score), which suffers catastrophic
        # cancellation and rounds to 0.0 as soon as Phi(z_score) rounds to 1.0 (around z > 8.3).
        return 0.5 * math.erfc(z_score / math.sqrt(2.0))
    
    
    def probability_sum_exceeds(t: int) -> float:
        """Estimate P(S > t) for the sum S of 1,000 fair 6-sided die rolls."""
        # Phase 1: Check physical bounds of S, which can only take integers in [1000, 6000].
        min_sum = NUM_ROLLS * 1
        max_sum = NUM_ROLLS * 6
    
        # Strictly '<': at t = 1000, all-ones gives S = 1000, so S > 1000 is NOT 100% certain.
        if t < min_sum:
            return 1.0
    
        # '>=': S can never exceed 6000, so even at t = 6000 the event S > 6000 is impossible.
        if t >= max_sum:
            return 0.0
    
        # Phase 2: Scale mean and variance from 1 roll to 1,000 independent rolls.
        sum_mean = NUM_ROLLS * SINGLE_ROLL_MEAN           # 3500.0
        sum_variance = NUM_ROLLS * SINGLE_ROLL_VARIANCE   # 8750 / 3 ~ 2916.67 (variances add by independence)
        sum_std_dev = math.sqrt(sum_variance)             # ~ 54.01 (grows with sqrt(NUM_ROLLS))
    
        # Phase 3: Continuity correction + standardization.
        # Because S is an integer, S > t is the exact same event as S >= t + 1.
        # On a continuous curve where integer k spans [k - 0.5, k + 0.5], the bar for t + 1 starts at t + 0.5.
        cutoff = t + 0.5
        z_score = (cutoff - sum_mean) / sum_std_dev
        return normal_upper_tail(z_score)
    

    ✅ Submit now — all tests should pass.

Intuition & how it works

Imagine tossing one die: the outcome jumps anywhere from 1 to 6. When you toss 1,000 dice and add them up, lucky 6s and unlucky 1s cancel out so consistently that the total clusters in a tight bell-shaped curve centered at 3,500. This is the Central Limit Theorem (CLT), which says that the sum of many independent, identically distributed random numbers with finite spread looks closely like a Gaussian (normal) bell curve. Before touching the bell curve, though, we enforce physical bounds: 1,000 dice must total at least 1,000 (all ones) and at most 6,000 (all sixes).

Computing the exact answer by listing every roll sequence is hopeless because 6^1000 has 779 digits. Yet plugging t blindly into a continuous bell-curve formula creates two bugs: it overlooks impossible/certain cutoffs outside [1000, 6000] (such as t = 900 or t = 6200), and it misplaces integer boundaries unless we apply continuity correction (shifting an integer cutoff by 0.5 so each whole number k owns the unit-width slice [k - 0.5, k + 0.5] beneath the continuous curve).

Let's see how this plays out using one toss's expected value (long-run average) 3.5 and variance (average squared deviation from the mean) 35/12, which scale across 1,000 independent tosses to mean = 3500, variance = 1000 * 35/12 ≈ 2916.67, and standard deviation (the square root of variance, measuring typical spread in sum points) sqrt(2916.67) ≈ 54.01:

  • t = 900: 900 lies below the minimum possible total 1,000, so S > 900 holds with probability 1.0 immediately.
  • t = 3500: S > 3500 on integers means S >= 3501, whose histogram slice begins at 3500.5. Converting 3500.5 into a z-score (how many standard deviations from the mean a cutoff sits) gives z = (3500.5 - 3500) / 54.01 ≈ +0.0093, leaving 0.4963 in the upper tail (slightly under 0.5 because S == 3500 does not count).
  • t = 3581: the slice for 3582 begins at 3581.5, giving z = (3581.5 - 3500) / 54.01 ≈ +1.5091 and upper-tail probability 0.0656 (6.56%).
  • t = 6200: 6,200 exceeds the maximum possible total 6,000, returning 0.0 immediately.

Complexity

  • Time: O(1). We execute two boundary comparisons, a handful of arithmetic operations, and one call to math.erfc (the complementary error function, a constant-time library routine that computes standard normal upper-tail areas directly); runtime never loops over the 1,000 tosses or depends on t.
  • Space: O(1). Only a fixed set of scalar floats (min_sum, max_sum, sum_mean, sum_variance, sum_std_dev, cutoff, z_score) is allocated.

Edge cases

  • Below the minimum possible total (t < 1000, including t = 900 and negative cutoffs): every outcome totals at least 1,000, returning 1.0. At t = 1000 itself, rolling a thousand 1s yields S = 1000, so S > 1000 is not certain (< rather than <=).
  • At or above the maximum possible total (t >= 6000, including t = 6200): no sequence can strictly exceed 6,000, returning 0.0 (>= rather than >). Without this guard, a continuous Gaussian curve assigns tiny positive density past the true maximum.
  • Right at the expected sum (t = 3500): returns ~0.4963 rather than 0.5, since S == 3500 (probability ~0.0074) is excluded by strict >.
  • Deep upper tail (t between 4000 and 5999): evaluating 1.0 - Phi(z) through the cumulative distribution function (CDF) Phi(z) (the probability that a standard normal variable is at most z) suffers catastrophic cancellation (loss of precision when subtracting two nearly equal floats) and collapses to 0.0 once z > 8.3 (t >= 3948). Calling 0.5 * math.erfc(z / sqrt(2)) preserves positive 64-bit floats all the way to t = 5576 (z ≈ 38.4).
  • Reflection symmetry: pairing each toss X_i with 7 - X_i proves P(S > t) + P(S > 6999 - t) = 1 for every integer t; the + 0.5 continuity correction preserves this identity to machine precision.

Common mistakes

  • Confusing variance with standard deviation. One toss has variance 35/12 ≈ 2.917 and standard deviation sqrt(35/12) ≈ 1.708; plugging 2.917 in as the standard deviation inflates the spread by 71%.
  • Multiplying standard deviation linearly by 1,000. Independent variances add (Var(S) = 1000 * Var(X)), so standard deviation scales by sqrt(1000) ≈ 31.62 to 54.01, not 1000 * 1.708 = 1708.
  • Omitting + 0.5 or shifting to t - 0.5. Using t (no correction) or t - 0.5 (which estimates P(S >= t) instead of P(S > t)) introduces an error of ~0.0037 to ~0.0074 near the center — more than 100x larger than the corrected approximation's peak error of ~0.000026.
  • Skipping support checks. Plugging t = 900 or t = 6200 straight into a z-score misses the physical bounds [1000, 6000].
  • Computing 1.0 - cdf(z) at large z. Rounds cdf(z) to 1.0 past z ≈ 8.3 and returns 0.0 prematurely.

Follow-up questions

  • What happens to the probability of exceeding 900 if only 150 rolls are performed instead of 1,000? With 150 tosses, the achievable totals span [150, 900], with expected sum 150 * 3.5 = 525 and standard deviation sqrt(150 * 35 / 12) ≈ 20.92. Now 900 sits right at the maximum achievable total (150 * 6 = 900) instead of below the minimum! Because no sequence of 150 tosses can strictly exceed 900, the probability flips from 1.0 all the way to 0.0 — whereas an unguarded Gaussian formula would wrongly report 1 - Phi((900.5 - 525) / 20.92) ≈ 2e-72 instead of exact zero (and if >= 900 were asked, only 150 straight sixes would qualify, with exact probability 6^-150 ≈ 4.9e-117).
  • Why does the Gaussian estimate lose relative accuracy in the extreme upper tail, and which analytical bounds or approximations work better there? The Central Limit Theorem guarantees small absolute error (< 3e-5 everywhere), so relative error blows up once the true tail probability shrinks far below 1e-5: the normal tail is ~0.9% high at t = 3700 (z ≈ 3.7), ~48% high at t = 4000 (z ≈ 9.3), more than 800x high at t = 4500, and positive even past 6,000. In deep tails, use a Chernoff bound (an exponential upper bound optimized over the moment-generating function E[e^(theta * X)] that respects bounded dice support), a saddlepoint approximation (an exponentially tilted normal refinement whose relative error stays tiny all the way to the boundary), or exact combinatorial counting.
  • How can you approximate the tail probability mentally—with no calculator or lookup table—when the cutoff sits roughly two standard deviations above the expected sum? Two standard deviations above 3,500 lands near 3500 + 2 * 54 = 3608. By the 68-95-99.7 empirical rule (which recalls that a normal bell curve contains roughly 68%, 95%, and 99.7% of its mass within 1, 2, and 3 standard deviations of the mean), about 5% of total probability lies outside ±2 standard deviations, and symmetry puts half of that in the upper tail: ~2.5%. Using the slightly sharper 95.45% figure for ±2 standard deviations gives (100% - 95.45%) / 2 ≈ 2.28%, right beside the continuity-corrected value 0.0223 (2.23%).
  • What algorithm computes the exact rational or floating-point value of P(S > t) across 1,000 rolls without relying on a normal curve? Two clean exact approaches work well: (1) dynamic programming (iteratively tabulating the exact integer count of roll sequences for every achievable sum from 1 toss up to 1,000 tosses using a 6-term sliding-window sum in roughly 6 * 1000^2 ≈ 6 * 10^6 integer additions), or (2) inclusion-exclusion (counting n-tuples of positive integers summing to at most m via C(m, n) and alternatingly subtracting/adding tuples where k dice exceed 6). Pairing X_i <-> 7 - X_i rewrites P(S > t) = P(S <= 7n - 1 - t), requiring at most (6000 - 1000) // 6 ≈ 834 binomial terms in Python's arbitrary-precision arithmetic:

```python import math

def exact_probability_exceeds(t: int, n: int = 1000) -> float: # Reflection symmetry X_i <-> 7 - X_i gives P(S > t) = P(S <= 7n - 1 - t). m = 7 * n - 1 - t if m < n: return 0.0 if m >= 6 * n: return 1.0 valid_sequences = 0 for k in range((m - n) // 6 + 1): ways = math.comb(n, k) * math.comb(m - 6 * k, n) valid_sequences += ways if k % 2 == 0 else -ways return valid_sequences / (6 ** n) ```

Follow-up questions

Similar questions

More Liftoff interview questions

All 6 Liftoff questions →