The interview problem
You must compare the expected costs of two service policies using a stochastic simulator. Your budget permits 10,000 evaluations of each policy. Explain how to estimate the difference, report uncertainty and make a reproducible parallel run. Can using the same random inputs improve precision? How much more compute is needed to halve the standard error?
Original fundamentals practice, not a reported company interview. Policies and numerical examples are invented. Primary documentation retrieved September 27, 2026; page publication dates unstated; interview date not applicable.
1. State the expectation being estimated
Let X be a scenario drawn from a specified distribution P, and let f(X) be its simulated cost. The target is μ = E[f(X)]. Define the scenario horizon, demand distribution, cost units, initial state and handling of failed runs before generating samples. Changing these changes the quantity being estimated.
Yᵢ = f(Xᵢ), with independent identically distributed scenarios μ̂ = (1/n) Σ Yᵢ E[μ̂] = μ Var(μ̂) = σ²/n, when Var(Yᵢ) = σ² is finite s² = Σ(Yᵢ − μ̂)² / (n − 1) estimated standard error = s / √n
The variance formula follows because independent observations have zero covariance and averaging divides the variance of their sum by n². An approximate fixed-sample interval is μ̂ ± 1.96s/√n when a suitable large-sample normal approximation applies. This is not a guarantee for every distribution or sample size. Heavy tails can make convergence slow; infinite variance invalidates this standard calculation.
Holding the distribution and cost per evaluation fixed, four times as many samples halves the standard error. More samples reduce Monte Carlo sampling error, not an incorrect demand model, a simulator bug or an omitted cost. An extremely precise estimate can still answer the wrong real-world question.
2. Establish an exact comparison baseline
Use a toy simulator whose truth is known. Let U be uniform on [0,1], and define costs A(U) = 10 + 4U and B(U) = 9 + 3U. Since E[U] = 1/2 and Var(U) = 1/12:
E[A] = 12 Var(A) = 16/12 = 4/3 E[B] = 10.5 Var(B) = 9/12 = 3/4 Δ = E[B − A] = −1.5
If the two policies use independent sets of n scenarios, the difference estimator is the mean of B minus the mean of A:
Var(Δ̂ independent) = Var(B)/n + Var(A)/n
= 25/(12n)
at n = 10,000: SE ≈ 0.014434
approximate 95% half-width using known variance ≈ 0.028290The negative target means B has lower expected cost in this simulator. The displayed half-width is a planning value using the known toy variance, not an interval from an actual run. A real report must include the observed estimate and its estimated uncertainty.
3. Pair scenarios to remove shared variation
Instead, evaluate both policies on the same U within each replication. Keep replications independent of one another. Define Dᵢ = B(Uᵢ) − A(Uᵢ), and estimate Δ by the mean of these n differences.
D = (9 + 3U) − (10 + 4U) = −1 − U Var(D) = 1/12 Var(Δ̂ paired) = 1/(12n) at n = 10,000: SE ≈ 0.002887 approximate 95% half-width using known variance ≈ 0.005658
The two designs each require 2n policy evaluations. Pairing reduces variance by a factor of 25 and standard error by a factor of 5 in this example. The reason is covariance:
Var(B − A) = Var(B) + Var(A) − 2Cov(A,B)
Cov(A,B) = 4 × 3 × Var(U) = 1
→ 3/4 + 4/3 − 2 = 1/12For real simulations, estimate the variance of the observed differences and report s_D/√n. Do not treat the 2n individual policy outputs as 2n independent differences. Pairing preserves each policy's marginal scenario distribution while allowing common difficulty to cancel in the comparison.
Sharing a seed is insufficient when policy code branches consume different numbers of random draws. The same stream position may represent an arrival in one policy and a service duration in the other. Prefer generating exogenous scenario data once per replication, or key draws by stable event identities. Verify that the coupling preserves the correct marginal model for each policy.
4. Know when shared randomness hurts
Now let A(U) = U and B(U) = 1 − U. Their covariance under shared U is negative. The paired difference is 1 − 2U, with variance 1/3. Independent policy outputs instead give difference variance 1/12 + 1/12 = 1/6.
Pairing doubles the variance here. Common random numbers benefit a difference estimator when they induce positive covariance between policy costs, not merely because the inputs are shared. Test the covariance and resulting variance on representative scenarios, and compare precision per unit of computation. A complicated coupling that saves variance but greatly increases evaluation time may be less efficient overall.
5. Use a known expectation as a control variate
Suppose the target is E[U²] = 1/3. We also know E[U] = 1/2. For a fixed coefficient β, define a corrected observation:
Z = U² − β(U − 1/2) E[Z] = E[U²] Var(Z) = Var(U²) + β²Var(U) − 2βCov(U²,U)
Minimizing this quadratic gives β* = Cov(U²,U)/Var(U). For uniform U:
Var(U²) = 1/5 − (1/3)² = 4/45 Cov(U²,U) = 1/4 − (1/3)(1/2) = 1/12 β* = 1 Var(Z) = 4/45 − 1/12 = 1/180
This reduces per-sample variance by a factor of 16. The control removes a predictable component without changing the target because its centered expectation is zero. Using an incorrect control expectation introduces bias. A coefficient estimated on the same observations needs additional care; a straightforward implementation estimates it on an independent pilot and fixes it for the production run. Conditional on that independently chosen coefficient, compute the production standard error from the corrected observations Z, rather than reusing the uncorrected variance.
Other methods, including stratification and randomized quasi-Monte Carlo, can help suitable problems. They change the sampling design and uncertainty calculation. In particular, dependent low-discrepancy points should not simply be treated as IID draws for an ordinary standard-error formula. SciPy's documentation discusses independent randomized replications for estimating uncertainty in randomized quasi-Monte Carlo.
6. Plan precision and handle rare events honestly
An independent pilot can estimate the standard deviation for a desired approximate half-width h. Use n ≈ (1.96s/h)² to plan a fixed production budget, while allowing for uncertainty in the pilot variance. This is a planning approximation, not a proof that the final interval will meet the requested width.
Repeatedly inspecting ordinary 95% intervals and stopping when the result looks favorable does not preserve their nominal sequential coverage. Use a justified sequential procedure or a prespecified production sample size. The same issue applies to stopping when a comparison first becomes significant.
Rare events can defeat an apparently reassuring standard error. If the true event probability is 0.001, the probability of seeing no events in 1,000 independent trials is:
P(no events) = (1 − 0.001)^1000 ≈ 0.3677
A zero empirical rate and a naive plug-in standard error of zero do not establish that the event is impossible. Use an appropriate binomial uncertainty calculation or an explicit rare-event sampling design. If importance sampling is introduced, the weights and their variance must be accounted for; counting biased samples as ordinary draws changes the target.
7. Build a reproducible simulation pipeline
target and simulator distribution specification
↓
independent scenario generation with stable IDs
↓
paired A/B evaluations on each shared scenario
↓
difference and centered-moment accumulation
↓
fixed-budget estimate and uncertainty report
↓
separate sensitivity analysis of simulator assumptionsUse independent streams for independent replications and intentional sharing only inside each policy pair. NumPy documents spawning child streams and deriving streams from deterministic unique identifiers. Allocate streams before dispatch or use an explicit identifier scheme. Accidentally cloning the same generator state into workers creates repeated scenarios, not additional independent evidence.
Record the root seed, generator and library versions, code version, model inputs and scenario-to-stream mapping. A seed alone does not fully describe a computation. Stable scenario identifiers help replay work independently of worker scheduling. Repeated jobs must be deduplicated by scenario ID rather than counted twice.
Maintain counts, means and centered sums of squares with a numerically stable online or mergeable algorithm. Avoid subtracting two very large nearly equal raw moments when computing a small variance. Combine unequal-sized worker batches using their counts, not an unweighted average of worker means.
Do not silently drop failed or timed-out scenarios. If difficult scenarios are slower, retaining only completed runs selects easier cases and can bias the estimate. Retry according to a defined policy, diagnose failures and report any unresolved coverage gap. A computational timeout is not automatically a valid zero-cost observation.
8. Validate the estimator and its assumptions
First test against the toy expectations and variances above. Check that paired inputs align, that scenario IDs are unique and that each policy's marginal input distribution remains correct. Across independent whole-run replications, compare empirical variability with reported standard errors. This diagnoses implementation mistakes; it does not prove that all future distributions are well behaved.
Inspect tails, extreme contributions and failed runs. Distinguish random sampling variation from deterministic numerical error, initialization effects and simulator misspecification. If a simulation uses a persistent Markov chain, consecutive states are not IID replications; its dependence and convergence require separate analysis.
Finally, report practical significance alongside statistical precision. A tiny cost improvement measured precisely may not justify operational complexity. Conversely, a wider interval may still be sufficient if every plausible outcome supports the same decision under the stated model.
9. Interview follow-ups
- Why not reuse one scenario many times? Repeating a deterministic result does not add independent information. If there is further internal randomness, identify the resulting hierarchy rather than pretending all outputs are IID.
- What if pairing increases variance? Inspect the covariance and event alignment; an independent design may be better.
- Can doubling samples halve error? Under the finite-variance IID mean calculation it multiplies standard error by 1/√2. Halving requires four times the sample count.
- The confidence interval is narrow but real outcomes disagree. Investigate the simulator distribution and implementation; Monte Carlo precision does not establish model validity.
- What if workers finish different numbers of scenarios? Merge their counts and centered statistics correctly, and investigate whether missing work is outcome-dependent.
Self-review rubric
A strong answer defines the target distribution, derives the mean's variance, uses the correct independent unit and explains square-root sample scaling. It computes paired uncertainty from differences, recognizes covariance can help or hurt, and separates simulation error from model error. An excellent answer also handles stopping, rare events, stream reuse, failed scenarios and cost-adjusted variance reduction.
Sources and attribution
- NumPy: Parallel random number generation — reproducible stream spawning and identifier-based stream construction.
- SciPy: Quasi-Monte Carlo submodule — sampling-design conditions and uncertainty through independent randomized replications.
Page publication dates unstated; retrieved September 27, 2026. The prompt, policy examples, algebra, calculations and implementation discussion are original practice material. No company interview attribution is claimed.
Continue your preparation
Choose a connected learning path or browse more ML fundamentals questions. Read our editorial method and attribution policy.