Geometry Summary Resource - aloalgo

Geometry Summary

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

ConceptFormulaUse Case
Euclidean Distancesqrt((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².

Rotate Image (Matrix Rotation)

Rotate 90° clockwise = transpose + reverse each row. Time: O(n²), Space: O(1).

Key Tips

  • Avoid sqrt: Use squared distance when comparing distances
  • K Closest: Max-heap of size K beats sorting for large inputs
  • Rectangle overlap: Think in terms of "when do they NOT overlap"
  • Valid square: 6 distances = 4 sides + 2 diagonals
  • Matrix rotation: Clockwise = transpose + reverse rows
Was this helpful?
© 2026 aloalgo. All rights reserved.