Check whether four unordered points form a square

Quick Overview

Given four points in the plane in no particular order, decide whether they are the vertices of a square with positive side length. The problem tests coordinate geometry with pairwise squared distances, handling unordered input and rotated squares, and catching degenerate cases such as repeated points.

Check whether four unordered points form a square

Company: HCL Technologies

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given the coordinates of four points in 2D space, `p1`, `p2`, `p3` and `p4`, return `True` if the four points construct a square, and `False` otherwise. The coordinate of a point is represented as `[x, y]`. 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). ### Function Signature ```python def valid_square(p1: list[int], p2: list[int], p3: list[int], p4: list[int]) -> bool: ``` ### Rules - Each point is a list of two integers `[x, y]`. - The four points can arrive in any order. Consecutive arguments are not necessarily adjacent corners. - The square may be rotated, so its sides do not have to be parallel to the axes. - If any two points coincide, return `False`, because a square needs sides of positive length. ### Constraints - `len(p1) == len(p2) == len(p3) == len(p4) == 2` - `-10^4 <= x, y <= 10^4` for every coordinate ### Examples ```text Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1] Output: True ``` The points are the corners of the unit square (0,0), (1,0), (1,1), (0,1), listed out of order. All four sides have length 1 and all four angles are 90 degrees. ```text Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,12] Output: False ``` The point (0,12) is far from the other three. The six squared distances between pairs are 1, 1, 2, 122, 144 and 145, so there is no set of four equal sides. ```text Input: p1 = [1,0], p2 = [-1,0], p3 = [0,1], p4 = [0,-1] Output: True ``` These points form a square turned 45 degrees around the origin. The four sides each have squared length 2 and the two diagonals each have squared length 4.

Overview: Given four points in the plane in no particular order, decide whether they are the vertices of a square with positive side length. The problem tests coordinate geometry with pairwise squared distances, handling unordered input and rotated squares, and catching degenerate cases such as repeated points.

HCL Technologies logo
HCL Technologies
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given the coordinates of four points in 2D space, p1, p2, p3 and p4, return True if the four points construct a square, and False otherwise.

The coordinate of a point is represented as [x, y]. 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).

Function Signature

def valid_square(p1: list[int], p2: list[int], p3: list[int], p4: list[int]) -> bool:

Rules

  • Each point is a list of two integers [x, y] .
  • The four points can arrive in any order. Consecutive arguments are not necessarily adjacent corners.
  • The square may be rotated, so its sides do not have to be parallel to the axes.
  • If any two points coincide, return False , because a square needs sides of positive length.

Constraints

  • len(p1) == len(p2) == len(p3) == len(p4) == 2
  • -10^4 <= x, y <= 10^4 for every coordinate

Examples

Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]
Output: True

The points are the corners of the unit square (0,0), (1,0), (1,1), (0,1), listed out of order. All four sides have length 1 and all four angles are 90 degrees.

Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,12]
Output: False

The point (0,12) is far from the other three. The six squared distances between pairs are 1, 1, 2, 122, 144 and 145, so there is no set of four equal sides.

Input: p1 = [1,0], p2 = [-1,0], p3 = [0,1], p4 = [0,-1]
Output: True

These points form a square turned 45 degrees around the origin. The four sides each have squared length 2 and the two diagonals each have squared length 4.