BackendML

Detect Squares

Medium3 min read
The Detect Squares problem asks us to design a data structure that supports adding points on a 2D plane and counting how many axis-aligned squares can be formed using a given query point as one of the corners.

Problem

Design a data structure that supports two operations: add a point to the data structure and count the number of axis-aligned squares that can be formed with a given point as one of the corners.

A square is considered valid when all four corners have the same side length and its sides are parallel to the x-axis and y-axis. Duplicate points are allowed, and their frequency must be considered when counting squares.

Example(s)

Consider the following example(s) to understand the expected input and output.

Input

add([3, 10])
add([11, 2])
add([3, 2])
count([11, 2])

Output

1

Solution

This solution uses HashMap to store the frequency of each point and another HashMap to group points by their x-coordinate. For a query point [x, y], we consider every other x-coordinate that contains points and use the horizontal distance between the two x-coordinates as the possible side length of the square.

For a candidate x-coordinate x2, the side length is side = |x2 - x|. The other two corners of the square must therefore have coordinates [x2, y + side] and [x, y + side], or [x2, y - side] and [x, y - side].

We check whether these three required points exist. The number of squares contributed by a configuration is the product of the frequencies of the three points because duplicate points can form multiple squares.

We use HashMap because point lookup needs to be efficient, while grouping points by x-coordinate allows us to consider only x-coordinates that actually contain points.
class DetectSquares {

    private Map<Integer, Map<Integer, Integer>> points;

    public DetectSquares() {
        points = new HashMap<>();
    }

    public void add(int[] point) {
        int x = point[0];
        int y = point[1];

        points.putIfAbsent(x, new HashMap<>());

        Map<Integer, Integer> yMap = points.get(x);
        yMap.put(y, yMap.getOrDefault(y, 0) + 1);
    }

    public int count(int[] point) {
        int x = point[0];
        int y = point[1];

        if (!points.containsKey(x)) {
            return 0;
        }

        int result = 0;

        for (int x2 : points.keySet()) {
            if (x2 == x) {
                continue;
            }

            int side = Math.abs(x2 - x);

            Map<Integer, Integer> yMap = points.get(x2);

            int count1 = yMap.getOrDefault(y + side, 0);
            int count2 = points.get(x).getOrDefault(y + side, 0);

            result += count1 * count2;

            count1 = yMap.getOrDefault(y - side, 0);
            count2 = points.get(x).getOrDefault(y - side, 0);

            result += count1 * count2;
        }

        return result;
    }
}

Complexity

The add operation performs a constant number of HashMap operations, resulting in an average time complexity of O(1).

For the count operation, we iterate through all distinct x-coordinates. If there are n distinct x-coordinates, the time complexity is O(n) on average because each required point can be looked up in a HashMap in O(1) average time.

The data structure stores every added point and its frequency, so the space complexity is O(n), where n is the number of points added.

Comments

No comments yet. Ask a question or share what helped.