KatInterview

Robot Navigation on a Hidden Grid with DFS and Backtracking

Trees & GraphsModel answer
MediumModel answerTrees & Graphsdfsbacktrackinggraph-traversal

Last updated

Guide a robot through a map it can only discover step by step, reaching the target with no repeated visits, real backtracking, and a clear result when the target is unreachable.

Problem

A vehicle is dropped into a map it has never seen. At each cell it can ask which compass directions are currently open, and it can drive one cell in any open direction. It cannot look ahead, and it has no global view of the map. Your job is to write the search routine that steers the vehicle to the goal without ever wandering in loops.

The setup was described informally, so part of the exercise is fixing the model and stating your assumptions out loud. Use this control interface (names are illustrative):

class Car:
    def open_directions(self) -> list[str]:
        """Directions ("N", "E", "S", "W") the car can drive from its current cell."""

    def move(self, direction: str) -> None:
        """Drive one cell in an open direction."""

    def at_destination(self) -> bool:
        """True when the current cell is the destination."""

Implement:

def find_destination(car) -> list[str] | None

The function must drive the car until it stands on the destination and return the list of directions that forms a route from the starting cell to the destination. If every reachable cell has been explored and the destination was never found, return None.

What is expected

  • State your model: grid cells, four directions, and what the car can and cannot sense.
  • Explain how you recognize a cell you have already been to, given that the car reports no coordinates.
  • Make sure the physical car, not just your data structures, is in the right place at every step of the search.
  • Argue that the algorithm terminates and analyze its cost in moves and memory.
  • Be ready to discuss whether the route must be shortest, and what changes if it must be.

Points worth clarifying

  • Is the map a grid, or a general network of junctions?
  • Does the car know its coordinates, or only the moves it has made?
  • Are all connections two-way (if A leads to B, does B lead back to A)?
  • Is any valid route acceptable, or the shortest one?
  • Is the total number of moves, including backtracking, something to minimize?

Hints

Hint 1

The car reports no coordinates, so build your own: start at (0, 0) and update a running position after every move. Use that position as the key in a visited set.

Hint 2

Depth-first search fits well because the car physically stands at one place. After exploring a neighbor, the car must drive back, so for each direction you need its opposite (N and S, E and W).

Hint 3

Keep the current route as a path of moves. Push a direction before recursing, and pop it when you backtrack. Return the path as soon as at_destination() is true, and return None if the whole search finishes without success.