Allowed R6 · Within policy
{
"slug": "nearest-stops-lookup",
"title": "Nearest Stops",
"difficulty": "medium",
"tags": [
"coding",
"technical",
"backend"
],
"is_free": true,
"locked": false,
"frequency": 0.75,
"last_reported_at": "2026-07-16T13:55:01.958172Z",
"url": "https://www.mokabla.com/en/c/swvl/nearest-stops-lookup",
"agent": {
"page": "https://www.mokabla.com/agent/en/c/swvl/nearest-stops-lookup",
"question": "https://www.mokabla.com/agent/_call/get_question?company=swvl&question=nearest-stops-lookup"
},
"company": {
"slug": "swvl",
"name_en": "Swvl"
},
"body_md": "## Problem\nA rider opens the app and we know their GPS position. We keep every pickup stop in a city in memory as\n`(id, lat, lng)`. Write `nearestStops(stops, rider, k, radiusKm)` that returns the ids of the `k` closest\nstops within `radiusKm` of the rider, closest first. Use great-circle (haversine) distance with an Earth\nradius of 6371 km. Cairo alone has about 40k stops and this runs on every app open, so once the naive\nversion works we will ask how to avoid scanning all of them.\n\n## Examples\n**Example 1**, `stops = [(1, 30.0444, 31.2357), (2, 30.05, 31.24), (3, 30.06, 31.22)]`,\n`rider = (30.045, 31.236)`, `k = 2`, `radiusKm = 5` → `[1, 2]`. Stop 1 is about 70 m away, stop 2 about\n680 m, stop 3 about 2.3 km but only two were requested.\n\n**Example 2**, same stops, `rider = (30.1, 31.4)`, `k = 3`, `radiusKm = 5` → `[]`. The nearest stop is\nroughly 16 km away, outside the radius.\n\n## Constraints\n- `1 ≤ len(stops) ≤ 50,000`, `1 ≤ k ≤ 50`, `0 < radiusKm ≤ 20`\n- Coordinates are valid WGS84 decimal degrees\n- Ties are broken by the lower stop id\n\n## What they look for\nA correct haversine (convert to radians!), not sorting all 40k candidates when `k` is small (a bounded\nheap or partial sort), and then the follow-up: how would you index this, geohash prefixes, an H3/S2 cell\ngrid, or a k-d tree, and what happens when the rider sits right on a cell boundary.",
"follow_ups_md": "- How would you keep the index fresh when ops adds or disables stops during the day?\n- The app calls this on every open; where do you cache and for how long?\n- Riders in Alexandria vs Cairo: one index or one per city?",
"access": "Question bodies are paid content: a person buys 30 days of access per company (200 EGP or 200 points) in the human app. Agents get titles, tags and difficulty; bodies only for free questions."
}