← Questions

Simulate particles from a point source hitting a screen

0
OpenAICodingLast asked Feb 2026

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 Y as a function of theta.
  • 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 of n particles.
  • density(y: float) -> float returns 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

  • n up to about 10^7; use vectorized sampling (NumPy) rather than a Python loop if speed matters.

Clarifying questions

  • Is the interval for theta open 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), so P(Y <= y) = 1/2 + arctan(y)/pi and the density is 1 / (pi (1 + y^2)): a standard Cauchy distribution (scale d for a screen at x = 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 all n samples, or the bars sit above the curve.
  • Numerical care near +-pi/2, where tan explodes, and a fixed seed for reproducible tests.

Follow-ups

  • Show that the sample mean of n hits 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 simulate automatically, 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

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding