Last Updated: February 3, 2026
Imagine you open the Uber app and request a ride. Within seconds, the app shows you several available drivers nearby.
Behind the scenes, Uber needs to search through millions of active drivers spread across the globe and find those within a few kilometers of your location. And it needs to do this thousands of times per second across all users.
How do you efficiently find "things near a point" when you have billions of location records?
A naive approach would scan every driver in the database and compute the distance to each one. With millions of drivers, this takes seconds per query, which is unacceptable for real-time applications.
This is where Quad Trees come in.
In this chapter, we'll explore:
Location-based services face a fundamental challenge: finding nearby entities quickly.
Consider these scenarios:
Let's say you store driver locations in a database table with latitude and longitude columns:
To find drivers within 5 km of a user at coordinates (37.7749, -122.4194), you might write:
This bounding box query has several problems:
The following diagram shows why B-tree indexes struggle with 2D queries:
What we need is a data structure that understands 2D space and can quickly eliminate irrelevant regions.