Valid Square - Problem
Given the coordinates of four points in 2D space p1, p2, p3 and p4, return true if the four points construct a square.
The coordinate of a point p_i is represented as [x_i, y_i]. The input is not given in any order.
A valid square has four equal sides with positive length and four equal angles (90-degree angles).
Input & Output
Example 1 — Valid Square
$
Input:
p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]
›
Output:
true
💡 Note:
The four points form a unit square. All sides have length 1, and both diagonals have length √2.
Example 2 — Invalid Square
$
Input:
p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,12]
›
Output:
false
💡 Note:
The points do not form a square because the sides have different lengths.
Example 3 — Collinear Points
$
Input:
p1 = [1,0], p2 = [2,0], p3 = [3,0], p4 = [4,0]
›
Output:
false
💡 Note:
All four points lie on the same line, so they cannot form a square.
Constraints
- The coordinates of all four points are integers
- -104 ≤ xi, yi ≤ 104
- All four points are distinct
Visualization
Tap to expand
Understanding the Visualization
1
Input
Four points in 2D space with coordinates
2
Process
Calculate distances and verify square properties
3
Output
Return true if points form a valid square
Key Takeaway
🎯 Key Insight: Check if 6 distances form exactly 4 equal sides and 2 equal diagonals with proper √2 ratio
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code