# Solving Leetcode Interviews in Seconds with AI: Valid Square


	# Introduction
	In this blog post, we will explore how to solve the LeetCode problem "593" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like [Chatmagic](https://www.chatmagic.app), we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

	# Problem Statement
	> 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  

	# Explanation
	Here's a breakdown of the solution and the Python code:

*   **Approach:**
    *   Calculate all possible distances between the four points. A square will have four equal sides and two equal diagonals.
    *   Store these distances and check if there are exactly four equal side lengths and two equal diagonal lengths. Ensure the side length is not zero (points are not the same).
    *   Use a set to efficiently count the number of distinct distances.

*   **Complexity:**
    *   Runtime Complexity: O(1) - constant time as the number of points is fixed at 4.
    *   Storage Complexity: O(1) - constant space.

	
	# Code
	```python
	def validSquare(p1, p2, p3, p4):
    """
    Determines if four points form a valid square.

    Args:
        p1: Coordinates of the first point [x1, y1].
        p2: Coordinates of the second point [x2, y2].
        p3: Coordinates of the third point [x3, y3].
        p4: Coordinates of the fourth point [x4, y4].

    Returns:
        True if the four points form a valid square, False otherwise.
    """

    def dist_sq(p1, p2):
        return (p1[0] - p2[0])**2 + (p1[1] - p2[1])**2

    distances = [
        dist_sq(p1, p2),
        dist_sq(p1, p3),
        dist_sq(p1, p4),
        dist_sq(p2, p3),
        dist_sq(p2, p4),
        dist_sq(p3, p4)
    ]

    distances.sort()

    # Check for four equal sides and two equal diagonals, and that side length is not 0
    return (distances[0] > 0 and
            distances[0] == distances[1] == distances[2] == distances[3] and
            distances[4] == distances[5] and
            distances[0] * 2 == distances[4])
	```
			
