← Questions

Blind maze escape: one command sequence for every start

0
GoogleCodingLast asked Apr 2026

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 row r, column c; row 0 is the top. U is row - 1, D row + 1, L column - 1, R column + 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 E it has escaped and ignores the remaining commands.
  • A sequence is valid if, from every . cell, the robot is on E after 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 one E; at most 30 open cells (. and E together).
  • Every . cell connects to E through 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 E drop out.
  • BFS over these sets from the set of all . cells, trying commands in the order D, 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^30 in 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 E greedily) 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

bfs · state space · grids

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