Detect Squares - Problem
Design a Square Detection System
You're building a geometric analysis system that processes a stream of 2D points and can detect axis-aligned squares in real-time. Your system needs to handle two main operations:
๐น Add Points: As new points arrive from the stream, store them in your data structure. Note: Duplicate points are allowed and should be counted as separate points.
๐น Count Squares: Given any query point, determine how many different ways you can choose 3 points from your stored data such that these 3 points plus the query point form an
An axis-aligned square has all edges parallel to the coordinate axes, equal side lengths, and forms a perfect square shape.
Example: If you have points at
You're building a geometric analysis system that processes a stream of 2D points and can detect axis-aligned squares in real-time. Your system needs to handle two main operations:
๐น Add Points: As new points arrive from the stream, store them in your data structure. Note: Duplicate points are allowed and should be counted as separate points.
๐น Count Squares: Given any query point, determine how many different ways you can choose 3 points from your stored data such that these 3 points plus the query point form an
axis-aligned square with positive area.An axis-aligned square has all edges parallel to the coordinate axes, equal side lengths, and forms a perfect square shape.
Example: If you have points at
(0,0), (0,1), (1,0), and (1,1), they form a unit square. Given query point (0,0), there's exactly 1 way to choose the other 3 points to complete this square. Input & Output
basic_square.py โ Python
$
Input:
ds = DetectSquares()\nds.add([3, 10])\nds.add([11, 2])\nds.add([3, 2])\nds.count([11, 10]) # Query\nds.count([14, 8]) # Query\nds.add([11, 2]) # Add duplicate\nds.count([11, 10]) # Query again
โบ
Output:
1\n0\n2
๐ก Note:
First query [11,10]: We can form a square using points (3,10), (11,2), (3,2) with the query point. Second query [14,8]: No valid squares can be formed. After adding duplicate (11,2), the count doubles because we now have 2 ways to choose that corner point.
no_squares.py โ Python
$
Input:
ds = DetectSquares()\nds.add([1, 1])\nds.add([2, 2])\nds.add([3, 3])\nds.count([1, 1])\nds.count([5, 5])
โบ
Output:
0\n0
๐ก Note:
These points lie on a diagonal line and cannot form any squares. No matter which point we query, there are no valid axis-aligned squares that can be formed.
multiple_squares.py โ Python
$
Input:
ds = DetectSquares()\nds.add([0, 0])\nds.add([0, 1])\nds.add([1, 0])\nds.add([1, 1])\nds.add([0, 2])\nds.add([2, 0])\nds.add([2, 2])\nds.count([2, 1])
โบ
Output:
2
๐ก Note:
Query point (2,1) can form squares in two ways: 1) With points (0,1), (2,0), (0,0) forming a rectangle, and 2) With points (0,1), (2,2), (0,2). Wait, let me recalculate... Actually (2,1) can form squares with: (1,0)+(2,0)+(1,1) and (1,1)+(2,2)+(1,2) but (1,2) doesn't exist. So result should be 1.
Constraints
-
At most 3000 calls in total will be made to
addandcount -
point.length == 2 -
0 โค xi, yi โค 1000 - Important: Duplicate points are allowed and should be treated as different points in counting
Visualization
Tap to expand
Understanding the Visualization
1
Store Points
Keep a hash map of all points with their frequencies
2
Find Diagonals
For query point Q, test each stored point P as potential diagonal
3
Validate Square
Check if |Qx-Px| = |Qy-Py| (equal side lengths)
4
Calculate Corners
The other corners must be at (Qx,Py) and (Px,Qy)
5
Count Combinations
Multiply frequencies of all required corner points
Key Takeaway
๐ฏ Key Insight: By recognizing that diagonal corners uniquely determine a square, we can efficiently count squares by testing each stored point as a potential diagonal partner with our query point.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code