A quick reference for geometry patterns in top interview problems.
Interview Focus
Geometry problems are rare in typical interviews. The most common patterns are: K Closest Points (distance + heap), Rectangle Overlap (coordinate comparison), Valid Square (distance validation), and Rotate Image (matrix manipulation).
Key Formulas
Concept
Formula
Use Case
Euclidean Distance
sqrt((x2-x1)² + (y2-y1)²)
Actual distance between points
Squared Distance
(x2-x1)² + (y2-y1)²
Comparing distances (faster)
Manhattan Distance
|x2-x1| + |y2-y1|
Grid movement (no diagonals)
K Closest Points to Origin
Use squared distance with a max-heap of size K. Time: O(n log k).
Rectangle Overlap
Check if projections overlap on both axes. Easier to check no-overlap conditions and negate.
Valid Square
Calculate all 6 pairwise distances. Valid square has 4 equal sides, 2 equal diagonals, and diagonal² = 2 × side².