Fast algorithm to find all points inside a rectangle
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Finding all points inside an axis-aligned rectangle is a standard spatial range-query problem. The fastest solution depends less on abstract asymptotic theory and more on workload shape: how many points exist, how often queries run, and whether the point set changes between queries. In many systems, the right answer starts with a simple scan and only moves to indexing when repeated queries justify the extra structure.
Define the Inclusion Rule First
Before optimizing anything, decide what "inside" means. A common rule is inclusive bounds:
x_min <= x <= x_max and y_min <= y <= y_max
That seems trivial, but boundary disagreements are a common source of false bug reports. If one component treats border points as included and another treats them as excluded, you can benchmark the perfect algorithm and still ship the wrong result.
A Linear Scan Is the Correct Baseline
For one query or a small point set, the simplest approach is usually the best.
This costs O(n) per query. That may sound slow, but it has zero setup cost and is hard to get wrong. Many real systems never need anything more complex.
Sort by One Axis for Repeated Queries
If the points are mostly static and rectangle queries happen often, a sorted index on one axis is a practical upgrade. Sort once by x, use binary search to find the candidate slice, and then filter those candidates by y.
This is often a good middle ground: much faster than a full scan for narrow queries, but far simpler than a full spatial tree.
Use Grid Bucketing for Uniform Spatial Data
When the points are spread fairly evenly over a large space and you expect many queries, a grid index can work well. Each point is assigned to a cell, and the query only touches the cells overlapped by the rectangle.
This can be very fast, but it depends on the data distribution. If most points fall into a few crowded cells, the benefit drops quickly.
Choose by Query Pattern, Not by Habit
A useful rule of thumb is:
- Few queries or constantly changing data: use a scan.
- Many queries on static data: use a sorted index or another reusable structure.
- Large, even spatial workloads: consider bucketing or a dedicated spatial tree.
That is more helpful than memorizing one "best" algorithm. Real performance depends on setup cost, memory overhead, update cost, and the shape of the rectangles you actually query.
Keep the Baseline for Verification
Once you add an index, keep the brute-force scan around as a correctness oracle. Randomized comparison tests are easy to write and catch subtle boundary bugs.
This is worth doing because an optimized spatial search that returns the wrong set of points is a regression, not an optimization.
Common Pitfalls
The most common mistake is jumping to a complex index before measuring whether a scan is already good enough. Another is leaving the boundary rule ambiguous. Grid approaches also fail when cell size is poorly chosen, and sorted one-axis indexes become awkward when the point set changes constantly.
Summary
- Rectangle point lookup is a range-query problem, and workload shape determines the best solution.
- A linear scan is the correct baseline and often the correct final answer.
- Sorting by one axis is a practical optimization for repeated queries on static data.
- Grid bucketing can help when spatial data is large and fairly uniform.
- Validate every optimized method against a simple brute-force implementation.

