Estimate pi with a seeded, vectorized Monte Carlo simulation in NumPy

Quick Overview

Implement a seeded, vectorized NumPy function that estimates pi by sampling points in the unit square and counting those inside the quarter circle. It tests reproducible random number generation, loop-free array code, memory limits for large samples, and how Monte Carlo error shrinks as the sample grows.

Estimate pi with a seeded, vectorized Monte Carlo simulation in NumPy

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `estimate_pi(n: int, seed: int) -> float`. It draws `n` points uniformly in the unit square, counts the points inside the quarter circle (`x**2 + y**2 <= 1`), and returns `4 * (inside / n)` as a `float`. The function must meet these requirements: - Use `rng = np.random.default_rng(seed)` as the only source of randomness. - Sample coordinates in `[0, 1)` with vectorized NumPy operations (no Python loop over points). - The same `seed` yields identical output on every run. ```python estimate_pi(1_000_000, seed=42) # -> a float close to 3.14, the same value on every call with seed=42 ``` ```hint Count without a loop A single generator call can return every coordinate at once as an `(n, 2)` array, and a boolean mask over that array can be counted directly. ``` ### Constraints & Assumptions - `1 <= n <= 10**8`. The draw must fit in memory; processing in chunks is optional. - No fixed accuracy tolerance is required, only determinism for a given seed. - NumPy is available and the generator comes from `np.random.default_rng`. ### What a Strong Answer Covers - Why four times the fraction of points inside the quarter circle estimates pi. - One seeded generator supplying both coordinates in a vectorized draw, and why that makes the output reproducible. - Returning a plain `float` with the integer count and the division handled correctly. - Time and memory cost near the upper bound of `n`, and when chunking becomes necessary. - The statistical behavior of the estimate: its expected value and how its error changes with `n`. ### Follow-up Questions - How would you handle very large `n` without exhausting memory? - How would you quantify the uncertainty of the estimate? - How many points do you need for a 95% confidence interval with a half-width of 0.001? - How could you reduce the variance of the estimate for the same number of random draws?

Overview: Implement a seeded, vectorized NumPy function that estimates pi by sampling points in the unit square and counting those inside the quarter circle. It tests reproducible random number generation, loop-free array code, memory limits for large samples, and how Monte Carlo error shrinks as the sample grows.

PracHub logo
General
Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement estimate_pi(n: int, seed: int) -> float. It draws n points uniformly in the unit square, counts the points inside the quarter circle (x**2 + y**2 <= 1), and returns 4 * (inside / n) as a float.

The function must meet these requirements:

  • Use rng = np.random.default_rng(seed) as the only source of randomness.
  • Sample coordinates in [0, 1) with vectorized NumPy operations (no Python loop over points).
  • The same seed yields identical output on every run.
estimate_pi(1_000_000, seed=42)
# -> a float close to 3.14, the same value on every call with seed=42

Constraints & Assumptions

  • 1 <= n <= 10**8 . The draw must fit in memory; processing in chunks is optional.
  • No fixed accuracy tolerance is required, only determinism for a given seed.
  • NumPy is available and the generator comes from np.random.default_rng .

What a Strong Answer Covers Guidance

  • Why four times the fraction of points inside the quarter circle estimates pi.
  • One seeded generator supplying both coordinates in a vectorized draw, and why that makes the output reproducible.
  • Returning a plain float with the integer count and the division handled correctly.
  • Time and memory cost near the upper bound of n , and when chunking becomes necessary.
  • The statistical behavior of the estimate: its expected value and how its error changes with n .

Follow-up Questions Guidance

  • How would you handle very large n without exhausting memory?
  • How would you quantify the uncertainty of the estimate?
  • How many points do you need for a 95% confidence interval with a half-width of 0.001?
  • How could you reduce the variance of the estimate for the same number of random draws?