← Questions
Blind maze escape: one command sequence for every start
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Google's onsite coding round, last asked Apr 2026. It tests bfs, state space and grids.
A robot is somewhere in a grid maze where # is a wall, . an open cell and E the exit. It obeys four commands, U, D, L and R, each moving it at most one cell. You do not know which . cell it starts on, and it gives no feedback while it runs, so you must fix the whole command sequence in advance. Find the shortest sequence that brings the robot to the exit wherever it started.
Interface
def blind_exit_sequence(maze: list[str]) -> str:
-
maze[r][c]is rowr, columnc; row 0 is the top.Uis row - 1,Drow + 1,Lcolumn - 1,Rcolumn + 1. - If the target cell is a wall or outside the grid, the robot stays put and the next command runs.
- Once the robot stands on
Eit has escaped and ignores the remaining commands. - A sequence is valid if, from every
.cell, the robot is onEafter some prefix of it. - Return a valid sequence of minimum length. Among those, return the smallest string, which orders the commands
D < L < R < U. If there is no.cell, return"".
Example
blind_exit_sequence(["#.E.#"]) -> "LR"
# start at column 3: L reaches E. Start at column 1: L hits the wall, R reaches E.
blind_exit_sequence(["####", "#.E#", "#..#", "####"]) -> "RU"
# (1,1): R exits. (2,1): R to (2,2), U exits. (2,2): R hits the wall, U exits.
# UR also works, but RU is smaller.
blind_exit_sequence(["#E#"]) -> ""
Constraints
-
1 <= rows, columns <= 10; exactly oneE; at most 30 open cells (.andEtogether). - Every
.cell connects toEthrough open cells, so a valid sequence exists.
Clarifying questions
- What happens on a move into a wall: does the robot stay, or does the command fail?
- Must the robot end on
E, or is it enough to pass over it? - Is any shortest sequence fine, or is there a tie-break?
What a strong answer covers
- The key idea: the state is the set of cells where a not-yet-escaped robot could be. Each command maps the set to a new set; robots that bump into walls merge, and robots that reach
Edrop out. - BFS over these sets from the set of all
.cells, trying commands in the orderD, L, R, U, until the set is empty. BFS gives the shortest length, and the command order with first-visit gives the smallest string of that length. - Representing a set as a bitmask over at most 30 cells, and a visited set of masks. The number of reachable sets is far smaller than
2^30in practice; say why. - Why per-robot shortest paths cannot simply be concatenated.
- Tests: a single
.cell, a corridor, two starts that need opposite commands.
Follow-ups
- Prove or argue that a valid sequence always exists under the connectivity guarantee.
- The maze is larger. Which heuristic (move the robot that is farthest from
Egreedily) gives a valid but not shortest sequence? - Some cells are slippery and move the robot two cells.
Where it's asked
- Google · Onsite coding · Last asked Apr 202645 min · Usually two rounds, mostly in person: whiteboard plus a Chromebook with Google Docs · No AI (a pilot code-comprehension round lets candidates use Gemini)
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.
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
