KatInterview

888. Mirror Reflection

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to determine which receptor (0, 1, or 2) a laser beam hits first. The beam starts from the southwest corner (0,0) of a square room of side length p and first hits the east wall at a height q.

Solution

Mirror Reflection in Python

class Solution:
    def mirrorReflection(self, p: int, q: int) -> int:
        # Continuously remove common factors of 2 from p and q.
        # This simplifies the problem without changing the final receptor.
        # The parities of the number of horizontal room extensions (m) and vertical extensions (n)
        # determine the receptor. This process preserves the parities of m and n.
        while p % 2 == 0 and q % 2 == 0:
            p //= 2
            q //= 2
        
        # After the loop, at least one of p or q is odd.
        # Let m_ext = p/gcd(p,q) and n_ext = q/gcd(p,q).

        if p % 2 == 0:
            # p is even, q must be odd.
            # m_ext = p/gcd(p,q) will be even (even/odd).
            # n_ext = q/gcd(p,q) will be odd (odd/odd).
            # m_ext is even, n_ext is odd -> hits wall x=0, y=p -> Receptor 2.
            return 2
        else: # p is odd
            if q % 2 == 1:
                # p is odd, q is odd.
                # m_ext = p/gcd(p,q) will be odd (odd/odd).
                # n_ext = q/gcd(p,q) will be odd (odd/odd).
                # m_ext is odd, n_ext is odd -> hits wall x=p, y=p -> Receptor 1.
                return 1
            else: # q is even
                # p is odd, q is even.
                # m_ext = p/gcd(p,q) will be odd (odd/odd).
                # n_ext = q/gcd(p,q) will be even (even/odd).
                # m_ext is odd, n_ext is even -> hits wall x=p, y=0 -> Receptor 0.
                return 0

Mirror Reflection in Java

class Solution {
    public int mirrorReflection(int p, int q) {
        // Continuously remove common factors of 2 from p and q.
        // This simplifies the problem without changing the final receptor.
        // The parities of the number of horizontal room extensions (m) and vertical extensions (n)
        // determine the receptor. This process preserves the parities of m and n.
        while (p % 2 == 0 && q % 2 == 0) {
            p /= 2;
            q /= 2;
        }

        // After the loop, at least one of p or q is odd.
        // Let m_ext = p/gcd(p,q) and n_ext = q/gcd(p,q).

        if (p % 2 == 0) {
            // p is even, so q must be odd.
            // m_ext = p/gcd(p,q) will be even (even/odd).
            // n_ext = q/gcd(p,q) will be odd (odd/odd).
            // m_ext is even, n_ext is odd -> hits wall x=0, y=p -> Receptor 2.
            return 2;
        } else { // p is odd
            if (q % 2 == 1) {
                // p is odd, q is odd.
                // m_ext = p/gcd(p,q) will be odd (odd/odd).
                // n_ext = q/gcd(p,q) will be odd (odd/odd).
                // m_ext is odd, n_ext is odd -> hits wall x=p, y=p -> Receptor 1.
                return 1;
            } else { // q is even
                // p is odd, q is even.
                // m_ext = p/gcd(p,q) will be odd (odd/odd).
                // n_ext = q/gcd(p,q) will be even (even/odd).
                // m_ext is odd, n_ext is even -> hits wall x=p, y=0 -> Receptor 0.
                return 0;
            }
        }
    }
}

Mirror Reflection in C++

class Solution {
public:
    int mirrorReflection(int p, int q) {
        // Continuously remove common factors of 2 from p and q.
        // This simplifies the problem without changing the final receptor.
        // The parities of the number of horizontal room extensions (m) and vertical extensions (n)
        // determine the receptor. This process preserves the parities of m and n.
        while (p % 2 == 0 && q % 2 == 0) {
            p /= 2;
            q /= 2;
        }

        // After the loop, at least one of p or q is odd.
        // Let m_ext = p/gcd(p,q) and n_ext = q/gcd(p,q).

        if (p % 2 == 0) {
            // p is even, so q must be odd.
            // m_ext = p/gcd(p,q) will be even (even/odd).
            // n_ext = q/gcd(p,q) will be odd (odd/odd).
            // m_ext is even, n_ext is odd -> hits wall x=0, y=p -> Receptor 2.
            return 2;
        } else { // p is odd
            if (q % 2 == 1) {
                // p is odd, q is odd.
                // m_ext = p/gcd(p,q) will be odd (odd/odd).
                // n_ext = q/gcd(p,q) will be odd (odd/odd).
                // m_ext is odd, n_ext is odd -> hits wall x=p, y=p -> Receptor 1.
                return 1;
            } else { // q is even
                // p is odd, q is even.
                // m_ext = p/gcd(p,q) will be odd (odd/odd).
                // n_ext = q/gcd(p,q) will be even (even/odd).
                // m_ext is odd, n_ext is even -> hits wall x=p, y=0 -> Receptor 0.
                return 0;
            }
        }
    }
};

Mirror Reflection in TypeScript

function mirrorReflection(p: number, q: number): number {
    // Continuously remove common factors of 2 from p and q.
    // This simplifies the problem without changing the final receptor.
    // The parities of the number of horizontal room extensions (m) and vertical extensions (n)
    // determine the receptor. This process preserves the parities of m and n.
    while (p % 2 === 0 && q % 2 === 0) {
        p /= 2;
        q /= 2;
    }

    // After the loop, at least one of p or q is odd.
    // Let m_ext = p/gcd(p,q) and n_ext = q/gcd(p,q).

    if (p % 2 === 0) {
        // p is even, so q must be odd.
        // m_ext = p/gcd(p,q) will be even (even/odd).
        // n_ext = q/gcd(p,q) will be odd (odd/odd).
        // m_ext is even, n_ext is odd -> hits wall x=0, y=p -> Receptor 2.
        return 2;
    } else { // p is odd
        if (q % 2 === 1) {
            // p is odd, q is odd.
            // m_ext = p/gcd(p,q) will be odd (odd/odd).
            // n_ext = q/gcd(p,q) will be odd (odd/odd).
            // m_ext is odd, n_ext is odd -> hits wall x=p, y=p -> Receptor 1.
            return 1;
        } else { // q is even
            // p is odd, q is even.
            // m_ext = p/gcd(p,q) will be odd (odd/odd).
            // n_ext = q/gcd(p,q) will be even (even/odd).
            // m_ext is odd, n_ext is even -> hits wall x=p, y=0 -> Receptor 0.
            return 0;
        }
    }
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.