• Sloth Bytes
  • Posts
  • 🦥 Geohashing: The Trick Behind Location Search

🦥 Geohashing: The Trick Behind Location Search

Sponsored by

Hello friends!

Welcome to this week’s Sloth Bytes. I hope you had an amazing week.

Learn AI in 5 minutes a day

This is the easiest way for a busy person wanting to learn AI in as little time as possible:

  1. Sign up for The Rundown AI newsletter

  2. They send you 5-minute email updates on the latest AI news and how to use it

  3. You learn how to become 2x more productive by leveraging AI

🦥 Geohashing: How Uber Finds Drivers Without Checking Everyone

You open Uber. Within seconds, it shows 5 nearby drivers. But Uber has millions of drivers worldwide. How did it find those 5 without checking everyone's location?

The answer is geohashing.

A clever trick that turns Earth into a searchable spreadsheet.

The Problem

Let's say you're building Uber. You have:

  • 5 million drivers online globally

  • User in San Francisco wants a ride

  • Need drivers within 2 miles

You could do a simple approach like this:

nearby_drivers = []
#loop through all 5 million drivers
for driver in all_5_million_drivers:
    if distance(user, driver) < 2:
        nearby_drivers.append(driver)

But that’s not a good idea…

That's 5 million distance calculations. Every. Single. Request.

And that’s not including the time it took to fetch the data of those 5 million drivers.

Your servers are now on fire and your users just missed whatever appointment they had waiting for a driver.

The Solution: World as a Grid

Geohashing converts GPS coordinates (latitude, longitude) into short strings:

  • San Francisco: (37.7749, -122.4194) → "9q8yyk"

Why is this effective?

Since you convert GPS coordinates into these short strings, nearby locations have similar geohashes.

  • Uber HQ: (37.7749, -122.4194) → "9q8yyk"

  • Starbucks next door: (37.7751, -122.4190) → "9q8yym" (similar)

  • Tokyo: (35.6762, 139.6503) → "xn774c" (completely different!)

Each character in the string represents the precision of location.

More characters = more precision.

Now, instead of having to loop through everything, you can use the string as a database index:

-- Before: 5 million distance calculations
-- After: Simple string match
SELECT * FROM drivers WHERE geohash LIKE '9q8yy%'

That's it.

If you want to increase/decrease the distance, all you have to do is modify the query to how many of the characters match the user’s location:

-- if you want drivers around 1 km of the user
SELECT * FROM driver WHERE geohash_6 = "<first_6_characters_of_users_geohash>"
-- if you want drivers around 5 km of the user
SELECT * FROM drivers WHERE geohash_5 = "<first_5_characters_of_users_geohash>"
-- if you want drivers around 10 km of the user
SELECT * FROM drivers WHERE geohash_4 = "<first_4_characters_of_users_geohash>"

Queries are faster and now when you loop through the items it’s no longer EVERYTHING, just whatever is nearby.

Who Uses This?

  • Uber/Lyft: Driver matching

  • Tinder: Finding matches 😉 

  • MongoDB: Geospatial indexes

  • Every delivery app: Restaurant search

If you’re interested in learning more check out this article:

Thanks for the feedback! I’ll do a game dev topic next week 😁 

Thanks to everyone who submitted!

This solution has a special place in my heart. Shoutout Miles!

Amazing solution

Birthday Cake Candles

You are in charge of the cake for a child's birthday. It will have one candle for each year of their total age. They will only be able to blow out the tallest of the candles.

Your task is to count how many candles are the tallest.

Examples

birthdayCakeCandles([4,4,1,3])
output = 2
// The tallest candles are 4. There are 2 candles with this height, so the function should return 2.
birthdayCakeCandles([1, 1, 1, 1])
output = 4
// All candles are the same height, so all are the tallest.
birthdayCakeCandles([])
output = 0
// No candles, so nothing to blow out.

How To Submit Answers

Reply with

  • A link to your solution (github, twitter, personal blog, portfolio, replit, etc)

  • or if you’re on the web version leave a comment!

  • If you want to be mentioned here, I’d prefer if you sent a GitHub link or Replit!

That’s all from me!

Have a great week, be safe, make good choices, and have fun coding.

If I made a mistake or you have any questions, feel free to comment below or reply to the email!

See you all next week.

What'd you think of today's email?

Login or Subscribe to participate in polls.

Want to advertise in Sloth Bytes?

If your company is interested in reaching an audience of developers and programming enthusiasts, you may want to advertise with us here.

Reply

or to participate.