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.

Stuck? AI Nudge Available

Get a conceptual hint to guide your logic without spoiling the final implementation.

03

Study the solution

The solution is waiting

Give it an honest attempt first — then compare your thinking with the full walkthrough.

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.