Magicsheet logo

Mirror Reflection

Medium
12.5%
Updated 8/1/2025

Mirror Reflection

What is this problem about?

The Mirror Reflection problem models a laser beam bouncing inside a square room with mirrors on three walls and receptors at specific corners. Given the room size p and the distance q the laser travels between reflections, determine which receptor the laser hits first. This Mirror Reflection coding problem is a beautiful application of number theory — specifically, the least common multiple and GCM reasoning.

Why is this asked in interviews?

Microsoft, Amazon, and Google ask this because it requires translating a geometric optics problem into a number theory problem. The key insight — that the laser hitting a receptor corresponds to LCM(p, q) determining the endpoint — rewards candidates who can think beyond simulation to mathematical structure. The math, number theory, and geometry interview pattern is elegantly demonstrated.

Algorithmic pattern used

LCM and parity reasoning. The laser effectively travels an unfolded path. After L = lcm(p, q) distance, the laser hits a corner. The number of room lengths traveled is L/p and the number of bounces is L/q. If L/p is even, the laser is at the bottom wall; if odd, at the top wall. If L/q is even, it's at the right receptor; if odd, at the left. Combine: receptor 0 (right-bottom = 0), receptor 1 (right-top), receptor 2 (left-top).

Example explanation

p = 3, q = 1.

  • LCM(3, 1) = 3.
  • Vertical steps: L/p = 3/3 = 1 (odd → top wall).
  • Horizontal steps: L/q = 3/1 = 3 (odd → left side? Wait — right wall always present).

Actually: L/p = 1 (odd = top), L/q = 3 (odd = right wall with receptor 1). Receptor = 1.

p = 2, q = 1: LCM=2. L/p=1 odd (top), L/q=2 even (right). Receptor 1 (top right) = 1.

Common mistakes candidates make

  • Simulating laser bounces step by step (can be slow for large p, q).
  • Confusing which receptor is at which corner (need to know receptor 0, 1, 2 positions).
  • LCM overflow for large p, q (use 64-bit integers).
  • Parity confusion: even vs odd number of reflections for horizontal/vertical placement.

Interview preparation tip

Geometry problems that involve periodic reflections often reduce to LCM/GCD reasoning. The "unfolding the room" trick — imagining the laser traveling in a straight line through mirrored copies of the room — transforms reflection into linear travel. The key math: lcm(p, q) = p * q / gcd(p, q). Practice similar "billiard ball" reflection problems using the unfolding technique — it appears in Google and math-heavy interviews and requires comfort with number theory fundamentals.

Similar Questions