AlgoMaster Logo

Chapter 10: Geometry

19 min readUpdated March 30, 2026
Listen to this chapter
Unlock Audio

Chapter 10: Geometry

Given a million points on a plane, find the two closest ones. The brute force approach checks every pair, giving you O(n^2). But with the right geometric insight, you can solve it in O(n log n). Geometry problems in interviews test whether you can translate spatial reasoning into clean, efficient code.

What makes geometry tricky is not the math itself. Most of it is high school level. The challenge is avoiding pitfalls like floating point errors, division by zero with vertical lines, and edge cases that silently break your logic. The good news is that a handful of core techniques, distance formulas, cross products, and the Shoelace formula, cover the vast majority of interview problems.

In this chapter, we will build up from basic distance calculations to orientation detection, line segment intersection, and convex hulls. By the end, you will have a solid toolkit for tackling any computational geometry problem that shows up in an interview.

Why It Matters

Geometry problems appear more often in interviews than most candidates expect. Problems like "Max Points on a Line," "Valid Square," "Rectangle Overlap," and "Minimum Area Rectangle" all require geometric reasoning. They test your ability to think spatially and then express that thinking in code.

Interview Insight: Interviewers use geometry problems to assess whether you can avoid common traps. The classic mistake on "Max Points on a Line" is using floating point slopes. Candidates who know the cross product trick immediately stand out.

Beyond interviews, computational geometry powers collision detection in games, geographic information systems, computer graphics, and robotics. The fundamentals we cover here are the same ones used in production systems worldwide.

Core Concepts

Distance Formulas

The two distance formulas you need to know are Euclidean distance and Manhattan distance.

Euclidean distance is the straight line distance between two points (x1, y1) and (x2, y2):

Manhattan distance (also called taxicab distance) measures distance along axis-aligned paths:

When should you use each? Euclidean distance is the default for "real" distance. Manhattan distance is useful in grid-based problems where you can only move horizontally or vertically, like a taxi navigating city blocks.

Interview Insight: When comparing Euclidean distances, you can often skip the square root entirely and compare squared distances instead. This avoids floating point issues and is faster. If d1^2 < d2^2, then d1 < d2.

Cross Product and Orientation

The cross product is the single most important tool in computational geometry. Given three points P1, P2, and P3, the cross product of vectors (P1 -> P2) and (P1 -> P3) tells you the orientation of the triplet.

The result tells you three things:

  • cross > 0: The points make a counterclockwise (left) turn
  • cross < 0: The points make a clockwise (right) turn
  • cross == 0: The points are collinear (on the same line)

This is powerful because it avoids division entirely. No division means no division-by-zero errors, no floating point imprecision, and no special-casing for vertical lines.

Collinearity Check

Three points are collinear if the cross product is zero. This is the correct way to check if points lie on the same line, not by computing and comparing slopes.

Why not slopes? Because slope = (y2 - y1) / (x2 - x1) breaks when x2 == x1 (vertical line) and introduces floating point errors when the coordinates are large. The cross product method works with integer arithmetic and handles all cases uniformly.

Detailed Explanation

Line Segment Intersection

Two line segments intersect if and only if they "straddle" each other. Segment AB straddles segment CD if points C and D are on opposite sides of line AB. And segment CD straddles segment AB if points A and B are on opposite sides of line CD.

We check this using orientation. For segments (P1, P2) and (P3, P4):

  1. Compute o1 = orientation(P1, P2, P3)
  2. Compute o2 = orientation(P1, P2, P4)
  3. Compute o3 = orientation(P3, P4, P1)
  4. Compute o4 = orientation(P3, P4, P2)

The segments intersect if:

  • o1 and o2 have different signs (P3 and P4 straddle the line through P1P2), AND
  • o3 and o4 have different signs (P1 and P2 straddle the line through P3P4)

There is a special case when one of the orientations is zero, meaning a point lies exactly on the other segment. In that case, we need to check if the point is within the bounding box of the segment.

Triangle Area (Shoelace Formula)

Given three vertices (x1, y1), (x2, y2), (x3, y3), the area of the triangle is:

This is a special case of the Shoelace formula, which computes the area of any polygon given its vertices. The absolute value is important because the raw formula gives a signed area (positive for counterclockwise, negative for clockwise).

The Shoelace formula generalizes to any polygon with n vertices:

where indices wrap around (vertex n is vertex 0).

Convex Hull

The convex hull of a set of points is the smallest convex polygon that contains all the points. Think of it as stretching a rubber band around all the points and letting it snap tight.

Andrew's monotone chain algorithm builds the convex hull in O(n log n):

  1. Sort the points by x-coordinate (break ties by y-coordinate).
  2. Build the lower hull by iterating left to right. For each point, while the last two points in the hull and the new point make a clockwise turn (or are collinear), remove the last point.
  3. Build the upper hull by iterating right to left with the same logic.
  4. Concatenate the lower and upper hulls.

The key operation in step 2 and 3 is the cross product check. If the cross product is non-positive (clockwise or collinear), we pop the last point from the hull.

Example Walkthrough

Detecting Orientation of Three Points

Let us work through orientation detection with concrete points.

Points: P1 = (0, 0), P2 = (4, 4), P3 = (1, 2)

Since cross > 0, the orientation is counterclockwise. P3 lies to the left of the line from P1 to P2.

Now let us try P3 = (2, 1):

Since cross < 0, the orientation is clockwise. P3 lies to the right of the line from P1 to P2.

Finally, P3 = (2, 2):

Cross product is zero, so the three points are collinear.

Checking Line Segment Intersection

Segment 1: A = (1, 1), B = (4, 4) Segment 2: C = (1, 4), D = (4, 1)

o1 and o2 have different signs (CCW and CW). o3 and o4 have different signs (CW and CCW). Therefore, the segments intersect.

Implementation

Below are implementations of the core geometric operations: Euclidean distance, Manhattan distance, cross product / orientation, collinearity check, and triangle area.

Java

Python

C++

C#

JavaScript

TypeScript

Complexity Analysis

OperationTime ComplexitySpace Complexity
Euclidean distanceO(1)O(1)
Manhattan distanceO(1)O(1)
Cross product / OrientationO(1)O(1)
Collinearity checkO(1)O(1)
Triangle areaO(1)O(1)
Line segment intersectionO(1)O(1)
Convex hull (Andrew's)O(n log n)O(n)
Closest pair of pointsO(n log n)O(n)

All the basic geometric primitives run in constant time. They are building blocks. The interesting complexity comes when you use these primitives inside larger algorithms. Convex hull is dominated by the initial sort. The closest pair algorithm uses divide and conquer, similar to merge sort, achieving O(n log n) by splitting points along the x-axis and cleverly limiting the cross-boundary comparisons.

Common Mistakes

1. Using Floating Point Slopes Instead of Cross Products

This is the most common mistake in geometry problems. When checking if points are collinear or computing slopes, developers reach for slope = (y2 - y1) / (x2 - x1). This breaks in two ways: division by zero when x1 == x2 (vertical line), and floating point precision errors when comparing slopes.

The fix: Always use cross products for collinearity and orientation. The cross product uses only multiplication and subtraction, so it stays in integer arithmetic when your coordinates are integers.

2. Integer Overflow in Cross Product Calculations

If coordinates can be up to 10^4, then the cross product involves multiplying differences that can be up to 2 10^4, and then multiplying two such values gives up to 4 10^8. That fits in a 32-bit integer. But if coordinates go up to 10^9 (which some problems allow), the intermediate products can reach 4 * 10^18, which overflows a 32-bit integer. You need long in Java, long long in C++, or similar.

The fix: Cast to long or long long before multiplying. This is why all the implementations above use explicit long casts.

3. Forgetting the Absolute Value in Area Calculations

The Shoelace formula gives a signed area. The sign depends on whether the vertices are listed clockwise or counterclockwise. If you forget the absolute value, you might get negative areas, which leads to wrong answers or, worse, answers that happen to pass some test cases but fail others.

The fix: Always wrap the result in abs() before dividing by 2.

4. Not Handling the Collinear Case in Segment Intersection

The general intersection test checks if orientations differ. But when points are collinear (cross product is zero), the test breaks down. Two collinear segments might overlap, or they might not. You need a separate check for the collinear case that verifies whether one endpoint lies within the bounding box of the other segment.

The fix: After detecting collinearity, check if the x and y projections of the segments overlap.

Common Interview Problems

Before we wrap up, let us briefly look at how these primitives apply to popular interview problems.

Max Points on a Line: For each pair of points, determine how many other points are collinear with them. Use the cross product (not slopes) to avoid floating point issues. Represent the direction as a reduced (dx, dy) pair using GCD, and count using a hash map. Time complexity: O(n^2).

Rectangle Overlap: Two axis-aligned rectangles overlap if and only if they overlap in both the x-projection and the y-projection. This reduces to checking if two intervals overlap, which is a one-dimensional problem.

Closest Pair of Points: Use divide and conquer. Sort by x, split into halves, solve recursively, then check the strip of width 2d around the dividing line (where d is the best distance so far). The strip check is O(n) because each point needs to be compared with at most 7 others in the strip.

Interview Insight: For the closest pair problem, the key insight that interviewers look for is why the strip check is O(n) and not O(n^2). The answer is that in a d x 2d rectangle, you can fit at most a constant number of points (at most 8) with pairwise distance at least d. So each point compares with at most 7 others.

Interview Questions

Q1: How would you check if three points are collinear without using division?

Use the cross product. Given points P1(x1, y1), P2(x2, y2), P3(x3, y3), compute:

If cross equals zero, the points are collinear. This avoids division entirely, so there is no risk of division by zero with vertical lines and no floating point precision issues. The computation uses only multiplication and subtraction, keeping everything in integer arithmetic when coordinates are integers.

Q2: What is the difference between Euclidean and Manhattan distance, and when would you use each?

Euclidean distance measures the straight-line distance between two points: sqrt((x2-x1)^2 + (y2-y1)^2). Manhattan distance measures the distance along axis-aligned paths: |x2-x1| + |y2-y1|. Use Euclidean distance for problems involving actual geometric distance (closest pair, circle intersection). Use Manhattan distance for grid-based problems where movement is restricted to horizontal and vertical steps, such as navigating a city grid or problems on a chessboard with rook movement. A useful optimization: when comparing Euclidean distances, compare squared distances to avoid the expensive and imprecise square root.

Q3: Explain how Andrew's monotone chain algorithm builds a convex hull.

The algorithm sorts points by x-coordinate (ties broken by y), then builds the hull in two passes. The lower hull is built left to right: for each new point, while the last two points and the new point make a clockwise turn (cross product is non-positive), remove the last point. This ensures only counterclockwise turns remain. The upper hull is built right to left with the same logic. Finally, the two halves are concatenated. The sorting step is O(n log n) and each point is added and removed at most once across both passes, so the hull construction is O(n). Total time: O(n log n). Total space: O(n) for the output hull.

Q4: Why is the Shoelace formula useful in competitive programming and interviews?

The Shoelace formula computes the area of any simple polygon given its vertex coordinates in O(n) time, using only addition, subtraction, and multiplication. It works for both convex and concave polygons. In interviews, it appears in problems asking for triangle areas (a special case with 3 vertices), checking if a point lies inside a polygon (by comparing the area of sub-triangles), and validating geometric properties. The formula avoids trigonometric functions entirely, making it both fast and numerically stable. The key thing to remember is to take the absolute value of the result, since the sign depends on the vertex ordering.

Summary

  • Euclidean distance measures straight-line distance. Compare squared distances when possible to avoid floating point issues.
  • Manhattan distance measures axis-aligned distance. Use it for grid-based movement problems.
  • The cross product is your most important tool. It determines orientation (clockwise, counterclockwise, collinear) without division.
  • Collinearity should always be checked with cross products, never with slope comparison.
  • Line segment intersection reduces to four orientation checks, plus a bounding box check for the collinear edge case.
  • The Shoelace formula computes polygon area in O(n) using only basic arithmetic. Always take the absolute value.
  • Convex hull can be computed in O(n log n) with Andrew's monotone chain. The core operation is the cross product check.
  • For the closest pair problem, divide and conquer achieves O(n log n). The strip check is efficient because only a constant number of points can exist in the critical region.
  • Always cast to long (or equivalent) before multiplying coordinates to avoid integer overflow.
  • Never use floating point slopes. The cross product is almost always the right tool.

References