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 pi
is represented as [xi, yi]
. 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).
Example 1:
Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1] Output: true
Example 2:
Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,12] Output: false
Example 3:
Input: p1 = [1,0], p2 = [-1,0], p3 = [0,1], p4 = [0,-1] Output: true
Constraints:
p1.length == p2.length == p3.length == p4.length == 2
-104 <= xi, yi <= 104
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The problem involves determining if four points in space form a perfect square. A brute force approach involves checking every possible combination of these points to see if they satisfy the properties of a square: equal sides and right angles.
Here's how the algorithm would work step-by-step:
def valid_square(point_one, point_two, point_three, point_four):
def calculate_distance_squared(first_point, second_point):
return (first_point[0] - second_point[0])**2 + (first_point[1] - second_point[1])**2
distances = []
distances.append(calculate_distance_squared(point_one, point_two))
distances.append(calculate_distance_squared(point_one, point_three))
distances.append(calculate_distance_squared(point_one, point_four))
distances.append(calculate_distance_squared(point_two, point_three))
distances.append(calculate_distance_squared(point_two, point_four))
distances.append(calculate_distance_squared(point_three, point_four))
distances.sort()
# Sides must be of equal length
if distances[0] == 0 or distances[0] != distances[1] or distances[0] != distances[2] or distances[0] != distances[3]:
return False
# Diagonals must be of equal length
if distances[4] != distances[5]:
return False
# Ensure the diagonal squared is twice the side squared
if distances[4] != 2 * distances[0]:
return False
return True
The goal is to determine if four given points form a valid square. The optimal approach focuses on using distances between the points to deduce the properties of a square, avoiding more complex geometric calculations.
Here's how the algorithm would work step-by-step:
import math
def valid_square(point_one, point_two, point_three, point_four):
def calculate_distance_squared(point_a, point_b):
return (point_a[0] - point_b[0])**2 + (point_a[1] - point_b[1])**2
distances = [
calculate_distance_squared(point_one, point_two),
calculate_distance_squared(point_one, point_three),
calculate_distance_squared(point_one, point_four),
calculate_distance_squared(point_two, point_three),
calculate_distance_squared(point_two, point_four),
calculate_distance_squared(point_three, point_four)
]
unique_distances = set(distances)
# A square must have exactly two unique distances: side and diagonal.
if len(unique_distances) != 2:
return False
unique_distances = list(unique_distances)
side_length_squared = min(unique_distances)
diagonal_length_squared = max(unique_distances)
# A square has four sides of equal length.
if distances.count(side_length_squared) != 4:
return False
# A square has two diagonals of equal length.
if distances.count(diagonal_length_squared) != 2:
return False
# Diagonal^2 should be twice the side^2 based on Pythagorean theorem.
if not math.isclose(diagonal_length_squared, 2 * side_length_squared):
return False
return True
Case | How to Handle |
---|---|
Null or empty input array | Return false immediately since a square requires four distinct points. |
Input array contains duplicate points (all four points are the same) | If all points are same, all distances will be zero and the code should return false because a square needs 4 DISTINCT points. |
Input array contains three collinear points | If three points are collinear, it is impossible to form a square, so the solution should return false after checking distances or slopes. |
Points do not form a square (e.g., a rectangle or rhombus) | The code must explicitly verify the equality of side lengths and diagonal lengths to ensure a square shape and return false if not equal. |
Integer overflow when calculating distance (large coordinates) | Use long data type to store the squared distances to prevent integer overflow, ensuring correct distance calculations. |
Points provided are not in any particular order | The distance calculation approach is order-independent and correctly identifies distances regardless of input order. |
Zero distance between two points (identical coordinates) | The code should check for zero distance between any two points and return false, since a square cannot have overlapping points. |
Floating-point precision issues when comparing distances (if using floating point) | Use an epsilon value (small tolerance) when comparing distances to account for potential floating-point inaccuracies. |