mediumMath & GeometryGoogle
Collinear Square Detector
A geometry service maintains a growing cloud of points and, on demand, counts axis-aligned squares that include a query point as one corner.
Design a data structure that supports add(point) and count(point): return the number of ways to pick 3 other recorded points to form an axis-aligned square whose fourth corner is point.
Choose approach
add(3,10)
0i
add(11,2)
1add(3,2)
2count(11,10)
3Mapcount
3,10→1
1/4
Narrationstep 1 of 4
add(3,10) → count=1.
Variables
step=0
op=add
x=3
y=10
How to think about it
- Keep count map for duplicate corners.
- Only candidate diagonals have |Δx|=|Δy|.
Edge cases to catch in interview
- Duplicates multiply the count.
- Degenerate (same point) excluded via Δx>0 check.
Examples
Example 1
1{"operations":[["add",[3,10]],["add",[11,2]],["add",[3,2]],["count",[11,10]]],"values":["add(3,10)","add(11,2)","add(3,2)","count(11,10)"]}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed