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 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 add and count
  • 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
Square Detection: Diagonal Corner StrategyStep 1: Query + Diagonal DetectionQPDiagonal?If |Qx-Px| = |Qy-Py|, then P can be diagonalStep 2: Calculate Required CornersQPC1(Qx,Py)C2(Px,Qy)Check if C1 and C2 exist in our hash map๐ŸŽฏ Key InsightFor any two diagonal corners of anaxis-aligned square, the other twocorners are uniquely determinedThis reduces our search from O(nยณ) to O(nยฒ)
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.
Asked in
DoorDash 15 Google 12 Amazon 8 Microsoft 6
23.8K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen