Simulate particles from a point source hitting a screen
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in OpenAI's agentic coding (beta) round, last asked Feb 2026. It tests simulation, probability and sampling.
A point source at (0, 0) fires particles at an infinitely tall vertical screen on the line x = 1. Each particle leaves at an angle theta drawn uniformly from (-pi/2, pi/2), measured from the positive x-axis, and travels in a straight line, so every particle hits the screen. Work out where the particles land, then confirm it with a simulation.
Part 1: the math
- Write the hit height
Yas a function oftheta. - Derive the CDF and the density of
Y.
Part 2: the simulation
Unless you agree on a different interface with the interviewer, assume:
-
simulate(n: int, seed: int | None = None) -> list[float]returns the hit heights ofnparticles. -
density(y: float) -> floatreturns the density you derived.
Plot a normalized histogram of the samples over the true density and show they agree.
Example
theta = pi/4 -> Y = 1
P(-1 <= Y <= 1) = 1/2 # half the angles lie in [-pi/4, pi/4]
simulate(10**6): share with |Y| <= 1 ~ 0.50
share with |Y| > 100 ~ 0.0064 # 2/pi * arctan(1/100)
Constraints
-
nup to about 10^7; use vectorized sampling (NumPy) rather than a Python loop if speed matters.
Clarifying questions
- Is the interval for
thetaopen or closed? What happens at exactly+-pi/2? - How far is the screen? Does the answer change if it is at
x = d? - Is the goal the derivation, the code, or both?
What a strong answer covers
-
Y = tan(theta), soP(Y <= y) = 1/2 + arctan(y)/piand the density is1 / (pi (1 + y^2)): a standard Cauchy distribution (scaledfor a screen atx = d). - Why it has no mean or variance: the tails fall off like
1/y^2, so the sample mean never settles while the median does (it stays near 0). - The histogram problem: a few huge samples stretch the axis, so clip to a range like
[-10, 10]and normalize by allnsamples, or the bars sit above the curve. - Numerical care near
+-pi/2, wheretanexplodes, and a fixed seed for reproducible tests.
Follow-ups
- Show that the sample mean of
nhits has the same distribution as a single hit. - The source now fires uniformly in all directions in 3D at a flat wall. What is the distribution of the hit point?
- How would you test
simulateautomatically, given that its output is random?
Where it's asked
- OpenAI · Agentic coding (beta) · Last asked Feb 2026An existing codebase and changes too large to write by hand; you work through them with an AI coding agent · An AI coding agent is expected
What it tests
simulation · probability · sampling
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
