Skip to solution
hardSystem Design

How would you design a 'nearby drivers' / proximity search?

822 views
01

Understand the problem

Outline geo search.

geoproximity
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Step 1: Outline use cases and constraints

Gather requirements and scope the problem. Ask questions to clarify use cases and constraints. Discuss assumptions.

Use cases

We'll scope the problem to handle only the following use cases

  • User performs core action described in How would you design a 'ne

Solution ready — 2 min read

Classified // press E to declassify

04

Read the code

Distance filter (haversine)
Run Playground
import math

def haversine(lat1, lon1, lat2, lon2):     # great-circle distance in km
    R = 6371
    p1, p2 = math.radians(lat1), math.radians(lat2)
    dphi = math.radians(lat2 - lat1)
    dlmb = math.radians(lon2 - lon1)
    a = math.sin(dphi/2)**2 + math.cos(p1)*math.cos(p2)*math.sin(dlmb/2)**2
    return 2 * R * math.asin(math.sqrt(a))

rider = (37.778, -122.415)
# In production you'd first narrow to the rider's geohash cell + neighbors,
# then compute exact distance only for that small candidate set:
drivers = {"d1": (37.776, -122.417), "d2": (37.781, -122.412), "d3": (37.760, -122.450)}

nearby = sorted(
    ((name, round(haversine(rider[0], rider[1], lat, lon), 2)) for name, (lat, lon) in drivers.items()),
    key=lambda x: x[1],
)
print("within 1 km:", [d for d in nearby if d[1] <= 1.0])
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 57 of 99 decoded in the System Design track. One more won't hurt.

Back to track