2013. Detect Squares
Practice Detect Squares for free on NudgeCode: Medium, Array & Hash Table pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.
Keep a collection of points on a grid. Adding the same point more than once keeps every copy.
add(point)storespoint = [x, y].count(point)returns how many ways you can choose three stored points that, together withpoint, form a square with positive area and sides parallel to the axes. Copies of the same point count as different choices.
Each test is a list of operations and their arguments; the expected output lists what each call returns (null for calls that return nothing).
Example
["DetectSquares","add","add","add","count","count","add","count"]
[[],[[3,10]],[[11,2]],[[3,2]],[[11,10]],[[14,8]],[[11,2]],[[11,10]]]
-> [null,null,null,null,1,0,null,2]
Constraints
- 0 <= x, y <= 1000
- Up to 3000 calls in total