Find a way out of a restricted map zone
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Perplexity's coding round, last asked Oct 2025. It tests search, geometry and bfs.
A map service marks some areas as premium zones. You are standing at a point inside one and need a way out. Given the starting latitude and longitude and a function that says whether a point is inside the zone, return a path of small steps from the start to a point outside it. The zone's shape is unknown and every call to the zone check costs time, so keep the number of calls low.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
in_zone(lat: float, lng: float) -> bool— given; may be slow (a network call). -
escape(start: tuple[float, float], step: float, in_zone, max_steps: int) -> list[tuple[float, float]] | None— a path starting atstartand ending at the first point outside the zone. Each move goes to one of the 8 neighbouring points on a grid of spacingstep(north, south, east, west and the diagonals). Return the path with the fewest moves;[start]if the start is already outside;Noneif no exit is found withinmax_stepsmoves.
Example
in_zone = lambda lat, lng: lat**2 + lng**2 < 1.0 # a disk of radius 1
escape((0.2, 0.0), step=0.25, in_zone, max_steps=50)
-> a path of 3 moves, for example
[(0.2, 0.0), (0.2, -0.25), (0.45, -0.5), (0.7, -0.75)] # distance 1.026 from the centre
# going straight in one direction takes 4 moves: (0.95, 0.0) is still inside
Constraints
-
stepis small next to the zone (a zone may be thousands of steps across). - Zones can be any shape, including non-convex ones with holes and narrow corridors.
- Calls to
in_zonedominate the cost: count them.
Clarifying questions
- Does the path have to be the shortest, or is any exit fine?
- Is a diagonal move allowed, and does it cost the same as a straight one?
- Should I handle the longitude wrap at ±180 and the poles?
- Can I call
in_zonein parallel or in batches? - Is the zone fixed while I search?
What a strong answer covers
- Breadth-first search outward from the start on the grid, with a visited set and a parent map to rebuild the path; it finds the fewest moves, at the cost of checking every grid point closer than the nearest exit (O(r^2) calls for exit distance
r). - Caching every
in_zoneresult so no point is checked twice. - Cheaper strategies when shortest isn't required: probe in 8 or 16 directions with doubling distances, then binary search the boundary along the first ray that leaves (O(log r) calls per direction), and explain where it fails (a narrow exit between rays, or a ray that leaves and re-enters).
- A
max_stepsor call budget so the search always stops. - Tests with a disk, a ring-shaped zone, and a zone with one narrow corridor.
Follow-ups
- Return the nearest point outside the zone instead of a grid path.
- The zone is given as a polygon. Compute the exit exactly instead of probing.
-
in_zonetakes 100 ms. Batch the frontier and query in parallel. - Steps must follow roads: search a road graph instead of a grid.
Where it's asked
- Perplexity · Coding · Last asked Oct 202545 min · Two live coding rounds
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
