Valid Square

MediumMathmath

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

You are given four points in the plane, p1, p2, p3, and p4, each as [x, y] with integer coordinates. The points are listed in no particular order. Return true if the four points are the corners of a square with **positive** side length, and false otherwise. The square does not have to be aligned with the axes. If two or more of the points coincide, the answer is false.

Examples

Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]
Output: true
These are the corners of the unit square.
Input: p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,12]
Output: false
The point `[0,12]` is far too high to close a square.
Input: p1 = [1,0], p2 = [-1,0], p3 = [0,1], p4 = [0,-1]
Output: true
A square rotated 45 degrees with side length `sqrt(2)`.

Constraints

  • `p1.length == p2.length == p3.length == p4.length == 2`
  • `-10^4 <= x, y <= 10^4`

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More math problems