Problem
Design a class that stores points on a 2D plane and counts squares. Each point is a list [x, y] of two integer coordinates.
Write the class DetectSquares with these operations: DetectSquares() creates an object with no points. add(point) stores point. The class keeps duplicate points, and each copy counts as a different point.
count(point) returns the number of ways to choose three stored points that make a square together with point. The count operation does not store point.
Each square must have a positive area. Its sides must be parallel to the x-axis and the y-axis.
Example 1
- Input:
- ["DetectSquares","add","add","add","count","count","add","count"] [[],[[0,0]],[[0,2]],[[2,0]],[[2,2]],[[1,1]],[[2,0]],[[2,2]]]
- Output:
- [null,null,null,null,1,0,null,2]
- Explanation:
- The first
count([2, 2])finds one square with the corners[0, 0],[0, 2]and[2, 0]. The point[1, 1]makes no square. After the secondadd([2, 0]), the two copies of[2, 0]give two squares.
Example 2
- Input:
- ["DetectSquares","add","add","add","add","count","count"] [[],[[4,4]],[[4,7]],[[7,4]],[[1,7]],[[7,7]],[[1,4]]]
- Output:
- [null,null,null,null,null,1,1]
- Explanation:
- Both squares have the side length 3. The point
[1, 4]makes a square with[1, 7],[4, 7]and[4, 4].
Constraints
- point.length == 2
- 0 <= x, y <= 1000
- At most 3000 calls in total to add and count
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
Sources · 1 reference
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 1 record
No company association
Leetcode 2013. Detect Squares
Reported date not specified · 0 source reports
Source snapshot: Nov 12, 2026
View source