Navigate a Maze Through a Next-Step API
RampSoftware Engineer · Technical ScreenLast updated
Design an algorithm to find an exit path in an unknown maze using an interactive, stateful API with backtracking.
Problem Statement
You are placed at an unknown starting position inside a 2D maze. You do not have direct access to the maze grid, dimensions, or layout. Instead, you interact with the environment strictly through a stateful MazeRunner interface.
Implement the function find_exit(runner: MazeRunner) -> list[str], which navigates the maze and returns an ordered list of move directions ('U', 'D', 'L', 'R') that leads from the starting cell to an exit cell. If no exit is reachable, return an empty list [].
MazeRunner Interface
class MazeRunner:
def step(self, direction: str) -> str:
"""
Attempts to move one cell in the given direction.
direction: 'U' (up), 'D' (down), 'L' (left), or 'R' (right).
Returns:
- 'WALL': A wall blocked the move. Runner position does NOT change.
- 'EMPTY': The move succeeded. Runner is now in the new cell.
- 'EXIT': The move succeeded and the runner is now at an exit cell.
"""
pass
def is_exit(self) -> bool:
"""
Returns True if the runner is currently on an exit cell, False otherwise.
"""
pass
Constraints
- The maze is finite with at most
1,000reachable empty cells. - Valid directions are
'U','D','L','R'. - Moving
'U'decreases the row coordinate (r - 1),'D'increases row (r + 1),'L'decreases column (c - 1),'R'increases column (c + 1). - The maze may contain cycles, dead ends, and disconnected regions.
- The starting position itself might already be an exit.
Examples
Example 1
# Maze layout (S = start, E = exit, # = wall, . = open):
# # # # #
# # S . #
# # # E #
# # # # #
# Execution:
runner.step('U') -> 'WALL'
runner.step('R') -> 'EMPTY'
runner.step('D') -> 'EXIT'
# Output:
['R', 'D']
Example 2
# Starting cell is already the exit
runner.is_exit() -> True
# Output:
[]
Edge Cases
- The runner starts directly on the exit (
is_exit()returnsTrueimmediately). - No exit exists in the reachable component (must backtrack fully and return
[]). - Loops and corridors with multiple intersections.
Hints
Hint 1
Because the runner physically maintains its position inside the maze, standard BFS is difficult without resetting; a depth-first search (DFS) with explicit backtracking is well-suited here.
Hint 2
Track visited cells using relative coordinates starting from (0, 0), so you avoid falling into infinite loops within cyclic paths.
Hint 3
Whenever a branch fails to reach the exit, you must reverse your last move using the opposite direction to restore the runner's physical position.
Follow-up questions
- How would you adapt your approach if you were required to find the shortest path to an exit rather than any valid path?
- Suppose each API call has network latency or a rate limit. How would you optimize the exploration strategy to minimize total API requests?
- What defensive measures would you take if the API could occasionally fail or return transient errors?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.