Ctrl K

Laser in a Mirror Room

Medium
A square room has side side, and all four walls are mirrors. A laser sits in the bottom left corner, and the other corners hold sensors: the bottom right one is number 0, the top right one is 1, the top left one is 2. The laser fires so that the beam first touches the right wall at height rise above sensor 0. From there it keeps bouncing off the walls until it runs into a corner. Return the number of the sensor the beam hits first. You may assume it always reaches some sensor.
Example 1
side=4, rise=1 → 2

The beam rises by 1 on every crossing between the left and right walls. It reaches height 4 after four crossings, which puts it back at the left wall, in corner 2.

Example 2
side=5, rise=2 → 0

After five crossings the beam is at the right wall, and the total rise of 10 is twice the room height, so it is at the bottom, in corner 0.

Example 3
side=3, rise=3 → 1

The beam runs straight along the diagonal and hits the top right corner 1 at once.

Constraints
  • 1 ≤ rise ≤ side ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSegments on a Chart MediumPrimes Below a Bound MediumArea Under Two Rectangles MediumNo Inward Corners MediumFour Pins Make a Square Medium
loading Python…
Input
side=4, rise=1
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 87 tests on the server, hidden ones included.