KatInterview

356. Line 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

Method Explanation

The problem asks if a given set of 2D points is symmetric with respect to a vertical line (a line parallel to the y-axis). This means that for every point (x, y) in the set, its reflection across this hypothetical line must also be in the set.

Solution

Line Reflection in Python

class Solution:
    def isReflected(self, points: List[List[int]]) -> bool:
        # If there are no points, it's trivially symmetric.
        if not points:
            return True

        point_set = set()
        min_x = float('inf')
        max_x = float('-inf')

        # First pass: find min/max X and populate a set with unique points.
        # In Python, tuples are hashable and can be stored in a set.
        for x, y in points:
            min_x = min(min_x, x)
            max_x = max(max_x, x)
            point_set.add((x, y))
        
        # The potential reflection line is at x = (min_x + max_x) / 2.
        # For a point (x, y), its reflection is (reflected_x, y) where
        # x + reflected_x = min_x + max_x.
        sum_x = min_x + max_x

        # Second pass: for each unique point, check if its reflection exists.
        for x, y in point_set:
            reflected_x = sum_x - x
            # If the reflected point is not in our set, then it's not symmetric.
            if (reflected_x, y) not in point_set:
                return False
        
        # If all points have a reflection, the set is symmetric.
        return True

Line Reflection in Java

class Solution {
    public boolean isReflected(int[][] points) {
        if (points == null || points.length == 0) {
            return true;
        }

        int minX = Integer.MAX_VALUE;
        int maxX = Integer.MIN_VALUE;
        // Use a HashSet to store unique points for O(1) average time lookups.
        // We serialize the point (x, y) to a string "x,y" to store in the set.
        Set<String> pointSet = new HashSet<>();

        // First pass: find min/max X and populate the set of unique points.
        for (int[] p : points) {
            minX = Math.min(minX, p[0]);
            maxX = Math.max(maxX, p[0]);
            pointSet.add(p[0] + "," + p[1]);
        }

        // The sum of x-coordinates for any pair of reflected points must be constant.
        // sumX = minX + maxX.
        int sumX = minX + maxX;

        // Second pass: for each original point, check if its reflection exists in the set.
        // Iterating through original points is fine, as the set contains all unique points from it.
        for (int[] p : points) {
            int x = p[0];
            int y = p[1];
            int reflectedX = sumX - x;
            String reflectedPointStr = reflectedX + "," + y;
            
            if (!pointSet.contains(reflectedPointStr)) {
                return false;
            }
        }

        return true;
    }
}

Line Reflection in C++

class Solution {
public:
    bool isReflected(vector<vector<int>>& points) {
        if (points.empty()) {
            return true;
        }

        int minX = points[0][0];
        int maxX = points[0][0];
        // Use a set of pairs to store unique points for efficient lookup.
        // std::pair has a default operator<, so it can be used in std::set.
        std::set<std::pair<int, int>> pointSet;

        // First pass: find min/max X and populate the set of unique points.
        for (const auto& p : points) {
            minX = std::min(minX, p[0]);
            maxX = std::max(maxX, p[0]);
            pointSet.insert({p[0], p[1]});
        }

        // The potential reflection line is at x = (minX + maxX) / 2.
        // For a point (x, y), its reflection is (reflected_x, y) where
        // (x + reflected_x) / 2 = (minX + maxX) / 2
        // x + reflected_x = minX + maxX
        // reflected_x = minX + maxX - x
        int sumX = minX + maxX;

        // Second pass: for each unique point, check if its reflection exists.
        for (const auto& p : pointSet) {
            int x = p.first;
            int y = p.second;
            int reflected_x = sumX - x;
            
            // If the reflected point is not in our set, then it's not symmetric.
            if (pointSet.find({reflected_x, y}) == pointSet.end()) {
                return false;
            }
        }

        // If all points have a reflection, the set is symmetric.
        return true;
    }
};

Line Reflection in TypeScript

function isReflected(points: number[][]): boolean {
    if (points.length === 0) {
        return true;
    }

    let minX = Infinity;
    let maxX = -Infinity;
    // Use a Set to store unique points for O(1) average time lookups.
    // We serialize the point (x, y) to a string "x,y" to store in the set.
    const pointSet = new Set<string>();

    // First pass: find min/max X and populate the set of unique points.
    for (const p of points) {
        minX = Math.min(minX, p[0]);
        maxX = Math.max(maxX, p[0]);
        pointSet.add(`${p[0]},${p[1]}`);
    }

    // The sum of x-coordinates for any pair of reflected points must be constant.
    // sumX = minX + maxX.
    const sumX = minX + maxX;

    // Second pass: for each unique point, check if its reflection exists.
    // We iterate through the set of unique points.
    for (const pStr of pointSet) {
        // We have to parse the string back to coordinates.
        const parts = pStr.split(',');
        const x = Number(parts[0]);
        const y = Number(parts[1]);
        
        const reflectedX = sumX - x;
        const reflectedPointStr = `${reflectedX},${y}`;

        if (!pointSet.has(reflectedPointStr)) {
            return false;
        }
    }

    return true;
};

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.