← Pure Storage Interview Insights
My first instinct was to do something with slopes and distances and I immediately started second-guessing myself on floating point precision.
Compute all six pairwise squared distances between the four points. For a valid square, these distances must consist of exactly four equal smaller values (the sides) and two equal larger values (the diagonals), with the diagonal being exactly twice the side. This method elegantly handles both axis-aligned and rotated squares without needing to check angles or slopes.
Pro tip: Mention that using squared distances avoids floating-point precision issues and that this approach is O(1) since the number of points is fixed. Also, clarify that the points must be distinct and form a non-degenerate square.
Confirm that the four points are distinct and that a valid square must have non-zero side length. Discuss how to handle duplicate points or collinear points.
Calculate the squared Euclidean distance between every pair of points (6 distances total). Use squared distances to avoid square roots and floating-point errors.
Sort the six distances or use a hash map to count frequencies. For a square, you should see exactly four equal smaller distances and two equal larger distances.
Check that the four smaller distances are equal, the two larger distances are equal, and the larger distance is exactly twice the smaller distance (diagonal^2 = 2 * side^2).
Return true if all conditions are met, false otherwise. Explain that the algorithm runs in O(1) time and space since the input size is fixed.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.