LeetCode Problem

How to Solve Mirror Reflection

The problem asks you to calculate the receptor where a laser first hits, given the room's side length (p) and the distance (q) from the 0th receptor. This involves understanding how the laser reflects in a mirrored room setup. The solution hinges on applying math and geometry concepts, particularly in how reflections work on walls and how the laser meets the receptors.

GhostInterview Help

Need help with Mirror Reflection without spending extra time grinding it?

GhostInterview can read Mirror Reflection from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #858Math plus GeometryReviewed 2026-03-08
Difficulty
Medium
Primary pattern
Math plus Geometry
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

The problem asks you to calculate the receptor where a laser first hits, given the room's side length (p) and the distance (q) from the 0th receptor. This involves understanding how the laser reflects in a mirrored room setup. The solution hinges on applying math and geometry concepts, particularly in how reflections work on walls and how the laser meets the receptors.

Problem Statement

In a square room with mirrors on all four walls, a laser is emitted from the southwest corner. The laser first strikes the east wall at a distance q from the 0th receptor located at the southeast corner. The room has walls of length p, and except for the southwest corner, receptors are placed at the other three corners numbered 0, 1, and 2. You are tasked with determining which receptor the laser ray meets first as it bounces around the room.

Given the integers p and q representing the room's size and the distance the laser travels before hitting the east wall, return the number of the receptor where the ray first strikes. The ray follows a path of reflections off the walls, and your goal is to identify the receptor the ray reaches first.

Examples

Example 1

Input: p = 2, q = 1

Output: 2

The ray meets receptor 2 the first time it gets reflected back to the left wall.

Example 2

Input: p = 3, q = 1

Output: 1

Example details omitted.

Constraints

  • 1 <= q <= p <= 1000

Solution Approach

Simulate the Laser Path

Simulate how the laser moves as it reflects off the walls of the room. Using the pattern of reflections, calculate the first intersection with a receptor. This approach involves tracking the laser's path based on the given room size and initial position.

Mathematical Reflection Model

A more efficient approach uses mathematical principles of reflection. By examining the laser’s path geometrically and understanding how it repeats after every complete reflection cycle, you can directly compute which receptor the laser will first meet.

Modular Arithmetic for Receptacle Determination

Leveraging modular arithmetic allows you to model the repeating reflection pattern. By simplifying the reflections into modular equations, you can quickly determine which receptor the laser will hit first based on p and q.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The time and space complexity of this problem depend on the chosen approach. A direct simulation can be slow due to repeated reflection cycles, while a mathematical or modular arithmetic solution significantly reduces the time complexity by solving the problem with fewer steps. The space complexity will primarily be determined by the number of variables used to track the laser's path and reflections.

What Interviewers Usually Probe

  • Look for understanding of geometry and modular arithmetic.
  • Check if the candidate approaches the problem by first simulating the reflections.
  • Evaluate whether the candidate considers optimizing the solution using math-based reflection patterns.

Common Pitfalls or Variants

Common pitfalls

  • Forgetting that the laser will eventually meet a receptor after a certain number of reflections, leading to inefficient simulation approaches.
  • Not fully understanding the geometric pattern of laser reflections and attempting brute-force methods.
  • Ignoring modular arithmetic’s role in optimizing the solution, leading to unnecessary calculations.

Follow-up variants

  • Different input values for p and q may lead to various patterns in laser reflections.
  • The problem can be extended to more complex room shapes or other configurations of mirrors.
  • You can explore performance optimizations by analyzing larger values for p and q.

How GhostInterview Helps

  • GhostInterview provides guidance on understanding and applying geometry and modular arithmetic concepts to efficiently solve this problem.
  • The platform helps you practice simulating laser reflections and applying the mathematical model to derive the correct answer.
  • By working through the solution steps with GhostInterview, you will improve your ability to recognize reflection patterns and optimize problem-solving approaches.

Topic Pages

FAQ

How does the Mirror Reflection problem involve math and geometry?

The problem combines principles of geometry and math, specifically reflection patterns and modular arithmetic, to find the receptor where the laser will hit first.

What is the best approach to solve the Mirror Reflection problem?

The most efficient solution involves using modular arithmetic to simulate the laser’s path and calculate the first receptor it hits, avoiding unnecessary simulations.

What are the common pitfalls when solving Mirror Reflection?

Candidates often struggle with inefficient brute-force solutions or fail to recognize the benefits of applying geometric and modular principles to the problem.

Can Mirror Reflection be solved using brute force?

While brute force is possible, it is inefficient, and solutions using geometry and modular arithmetic are much faster and scalable.

How can GhostInterview help with Mirror Reflection practice?

GhostInterview aids by walking through the core concepts of laser reflections, offering solutions that highlight optimization techniques like modular arithmetic.

GhostInterview Solver

Need direct help with Mirror Reflection instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Mirror Reflection from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.