Design a point store that counts axis-aligned squares formed with a query point
Quick Overview
Design a class that stores a stream of 2D points, duplicates included, and counts how many ways three stored points complete an axis-aligned square with a given query point. It tests hash map design, reasoning about square corners, and careful counting of repeated points.
Design a point store that counts axis-aligned squares formed with a query point
Company: Square
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
You are given a stream of points on the X-Y plane. Design a data structure that supports adding new points from the stream and counting the number of ways to choose three points from the data structure such that the three points and a given query point form an **axis-aligned square** with positive area.
An axis-aligned square is a square whose edges are all the same length and are either parallel or perpendicular to the x-axis and the y-axis. Positive area means the side length is greater than zero.
Implement the `DetectSquares` class:
- `DetectSquares()` creates an empty structure.
- `add(point: List[int]) -> None` adds a new point `[x, y]` to the data structure. Duplicate points are allowed and should be treated as different points.
- `count(point: List[int]) -> int` counts the number of ways to choose three points from the data structure such that the three points and the query point `[x, y]` form an axis-aligned square with positive area.
The query point does not have to be in the structure, and `count` does not add it.
Example 1:
```text
ds = DetectSquares()
ds.add([3, 10])
ds.add([11, 2])
ds.add([3, 2])
ds.count([11, 10]) # returns 1
ds.count([14, 8]) # returns 0
ds.add([11, 2])
ds.count([11, 10]) # returns 2
```
The first query finds one square, with corners (3, 10), (11, 2), (3, 2) and (11, 10). No three stored points form a square with (14, 8). After (11, 2) is added a second time, the same square can use either copy of (11, 2), so the last query returns 2.
Example 2:
```text
ds = DetectSquares()
ds.add([0, 0])
ds.add([0, 1])
ds.add([1, 0])
ds.add([1, 1])
ds.count([0, 0]) # returns 1
ds.count([1, 1]) # returns 1
```
Both queries find the unit square with corners (0, 0), (0, 1), (1, 0) and (1, 1), once from each of two opposite corners. In the first query the stored copy of (0, 0) is not one of the three chosen points, because the query point already fills that corner.
```hint Where the other corners can be
Look at the stored points that share the query's x-coordinate. Choosing one of them fixes the side length. How many squares are left to check?
```
```hint Repeated points
Keep a count for each coordinate and multiply the counts of the three corners. Check that your loop never visits the same coordinate twice in one query.
```
### Constraints & Assumptions
- `point.length == 2`
- `0 <= x, y <= 1000`
- At most 3000 calls in total are made to `add` and `count`.
### Clarifying Questions to Ask
- If the query point was also added earlier, should those stored copies change the result?
- Will `count` be called much more often than `add`, or the reverse? That decides which operation should carry the work.
- Will coordinates stay in the 0 to 1000 range, or should the design also handle large or negative coordinates?
### What a Strong Answer Covers
- A storage layout that records how many times each coordinate was added and finds the possible corner points for a query without scanning every stored point.
- An argument that each square containing the query point is counted exactly once, including squares on both sides of the query.
- Correct handling of duplicate points and of zero-length sides.
- The time and space cost of `add` and `count`, and which of the two the design makes cheap.
### Follow-up Questions
- How would you support removing a point?
- How would you count squares that do not have to be axis-aligned?
- If `count` calls far outnumber `add` calls, what could you precompute in `add`, and what would it cost in memory?
- How would you count axis-aligned rectangles that have the query point as one corner?
Overview: Design a class that stores a stream of 2D points, duplicates included, and counts how many ways three stored points complete an axis-aligned square with a given query point. It tests hash map design, reasoning about square corners, and careful counting of repeated points.
You are given a stream of points on the X-Y plane. Design a data structure that supports adding new points from the stream and counting the number of ways to choose three points from the data structure such that the three points and a given query point form an axis-aligned square with positive area.
An axis-aligned square is a square whose edges are all the same length and are either parallel or perpendicular to the x-axis and the y-axis. Positive area means the side length is greater than zero.
Implement the DetectSquares class:
DetectSquares()
creates an empty structure.
add(point: List[int]) -> None
adds a new point
[x, y]
to the data structure. Duplicate points are allowed and should be treated as different points.
count(point: List[int]) -> int
counts the number of ways to choose three points from the data structure such that the three points and the query point
[x, y]
form an axis-aligned square with positive area.
The query point does not have to be in the structure, and count does not add it.
The first query finds one square, with corners (3, 10), (11, 2), (3, 2) and (11, 10). No three stored points form a square with (14, 8). After (11, 2) is added a second time, the same square can use either copy of (11, 2), so the last query returns 2.
Both queries find the unit square with corners (0, 0), (0, 1), (1, 0) and (1, 1), once from each of two opposite corners. In the first query the stored copy of (0, 0) is not one of the three chosen points, because the query point already fills that corner.
Constraints & Assumptions
point.length == 2
0 <= x, y <= 1000
At most 3000 calls in total are made to
add
and
count
.
Clarifying Questions to Ask Guidance
If the query point was also added earlier, should those stored copies change the result?
Will
count
be called much more often than
add
, or the reverse? That decides which operation should carry the work.
Will coordinates stay in the 0 to 1000 range, or should the design also handle large or negative coordinates?
What a Strong Answer Covers Guidance
A storage layout that records how many times each coordinate was added and finds the possible corner points for a query without scanning every stored point.
An argument that each square containing the query point is counted exactly once, including squares on both sides of the query.
Correct handling of duplicate points and of zero-length sides.
The time and space cost of
add
and
count
, and which of the two the design makes cheap.
Follow-up Questions Guidance
How would you support removing a point?
How would you count squares that do not have to be axis-aligned?
If
count
calls far outnumber
add
calls, what could you precompute in
add
, and what would it cost in memory?
How would you count axis-aligned rectangles that have the query point as one corner?