Light Source Hits: Sample, Compare, and Fit
Problem statement
A point light source sits at (px, py) with py > 0, above a straight line, the x-axis, which plays the role of a one-dimensional screen. Each photon leaves the source in a random direction. Measure its direction by the angle θ from straight down: the photons that reach the line have θ spread uniformly over (-π/2, π/2), and such a photon hits the line at x = px + py · tan(θ).
Examples
Example 1
task = "SAMPLE"px = 1.0py = 2.0values = [0.5,0.75,0.25,0.9]return = [1.0,3.0,-1.0,7.15536707435]The angles are π · (u - 1/2): 0, π/4, -π/4 and 0.4π. Their tangents are 0, 1, -1 and about 3.0776835, so the hits are 1 + 2 · tan: 1, 3, -1 and about 7.1553671, in the order of the uniform numbers.
Unlock this recently reported problem
FastPrep Pro gives you full access to interview problems reported within the last week.
- Full problem statement and constraints
- 2 more worked examples, explained
- Guided hints and editorial
- Run your code on real test cases
$99 billed yearly — or $19 month-to-month. Cancel anytime.