Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Compute Pi with RNG
00:00
5 left

Compute Pi with RNG

MediumPython

Problem

DeepMind research evaluation code sometimes needs a reproducible numerical baseline. Implement an adaptive Monte Carlo estimator for pi by sampling points uniformly from the unit square and measuring how many fall inside the quarter circle.

Use the specified 32-bit linear congruential generator rather than Python's global random state. For each sample, generate two values in [0, 1), compute x² + y², and classify the point as inside when the value is at most 1.

After each sample, estimate pi as 4 * inside / samples. Also compute a conservative 95% confidence half-width:

4 * 1.96 * sqrt((p * (1 - p) + 1 / samples) / samples), where p = inside / samples.

Stop early when this half-width is at most tolerance. Otherwise, stop after max_samples samples. Return a dictionary containing the estimate rounded to six decimal places, the number of samples used, and whether the tolerance was reached.

Formal Specification

Implement estimate_pi(tolerance, max_samples, seed). tolerance is a positive float, max_samples is a positive integer, and seed is a nonnegative integer. The output is {"estimate": float, "samples": int, "converged": bool}.

The generator state starts at seed. Each update is state = (1664525 * state + 1013904223) mod 2^32, and each generated value is state / 2^32.

Constraints

  • 0 < tolerance <= 1
  • 1 <= max_samples <= 10^8
  • 0 <= seed < 2^32
  • The generator state is updated modulo 2^32
  • Do not store sampled points

Function Signature

def estimate_pi(tolerance, max_samples, seed):
Interviewer

Your question is Compute Pi with RNG. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.