Determine whether points form squares
Problem: Square Detection and Counting from Point Coordinates
You are given points on a 2D plane with integer coordinates.
Part A — Validate a Single Square
Given exactly 4 points [(x1,y1), (x2,y2), (x3,y3), (x4,y4)], determine whether these 4 points can be the vertices of a non-degenerate square. The square may be rotated, meaning its sides are not necessarily axis-aligned.
Output: true if they form a square, otherwise false.
Part B — Count Squares in a Set with Optimization
Given a set of N points, which may contain duplicates, count how many distinct squares can be formed whose 4 vertices are all present in the given set.
- A square is considered the same regardless of the order of its vertices.
- Squares may be rotated.
- Duplicates in the input set should be treated as separate inputs, but they should not create degenerate "squares". This implies that the four vertices forming a square must be distinct coordinate pairs in space, even if the input array contains multiple identical coordinate pairs.
Follow-up:
- Describe a brute-force approach, typically around O(N^4) time complexity.
- Improve this approach to achieve approximately O(N^3) time complexity.
- Further improve to achieve approximately O(N^2) time complexity. Assume hash set or hash map lookups have an average time complexity of O(1).
Constraints
1 <= N <= 2e4(for the optimized discussion in Part B)- Coordinates fit within 32-bit signed integers.
Clarify any additional assumptions you need, such as the exact interpretation of duplicates.
AI interview prep
Core dilemma
Efficiently identifying all distinct geometric patterns (squares) within a large set of points, handling arbitrary rotations, and optimizing lookup performance while correctly interpreting duplicate input points.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Determine whether points form squares
Problem: Square Detection and Counting from Point Coordinates
You are given points on a 2D plane with integer coordinates.
Part A — Validate a Single Square
Given exactly 4 points [(x1,y1), (x2,y2), (x3,y3), (x4,y4)], determine whether these 4 points can be the vertices of a non-degenerate square. The square may be rotated, meaning its sides are not necessarily axis-aligned.
Output: true if they form a square, otherwise false.
Part B — Count Squares in a Set with Optimization
Given a set of N points, which may contain duplicates, count how many distinct squares can be formed whose 4 vertices are all present in the given set.
- A square is considered the same regardless of the order of its vertices.
- Squares may be rotated.
- Duplicates in the input set should be treated as separate inputs, but they should not create degenerate "squares". This implies that the four vertices forming a square must be distinct coordinate pairs in space, even if the input array contains multiple identical coordinate pairs.
Follow-up:
- Describe a brute-force approach, typically around O(N^4) time complexity.
- Improve this approach to achieve approximately O(N^3) time complexity.
- Further improve to achieve approximately O(N^2) time complexity. Assume hash set or hash map lookups have an average time complexity of O(1).
Constraints
1 <= N <= 2e4(for the optimized discussion in Part B)- Coordinates fit within 32-bit signed integers.
Clarify any additional assumptions you need, such as the exact interpretation of duplicates.