← Questions

Find a way out of a restricted map zone

0
PerplexityCodingLast asked Oct 2025

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 at start and ending at the first point outside the zone. Each move goes to one of the 8 neighbouring points on a grid of spacing step (north, south, east, west and the diagonals). Return the path with the fewest moves; [start] if the start is already outside; None if no exit is found within max_steps moves.

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

  • step is 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_zone dominate 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_zone in 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_zone result 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_steps or 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_zone takes 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

What it tests

search · geometry · bfs

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding