Universal Exit Command Sequence
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The edge case that kills a naive solution here is the wall bump. A move into a wall doesn't kill a possible start, it just leaves it standing, and that's what makes this Google OA from September 2026 nastier than it looks. You're not walking one path. You're steering a whole set of possible starts with one command string, and the set shrinks as positions merge or hit E. Up to 20 dot cells is a loud hint about the state space. If you blank on the state design during the live assessment, StealthCoder is the invisible safety net that reads the problem and hands you the approach.
The problem
A rectangular maze uses # for walls,. for passable cells that may be the unknown start, and exactly one E exit. You must choose one fixed command string over U, D, L, and R. Every possible start executes every command without feedback. A move into a wall or outside the grid leaves that possibility in place. A possibility that enters E terminates and is removed. Return the shortest command string that makes every possible start reach the exit. Break ties by exploring commands in U,D,L,R order. Function universalExitCommands(grid: String[]) → String Examples Example 1 grid = ["..E"] return = "RR" Two right moves merge and then exit both possible starts. Example 2 grid = ["..","E#"] return = "LD" A horizontal merge followed by a downward move reaches the exit. Constraints 1 <= rows, columns <= 20. The grid is rectangular and contains exactly one E. There are at most 20 dot cells. A universal sequence is guaranteed to exist.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to treat the set of still-possible positions as the state. With at most 20 dot cells, encode that set as a bitmask of up to 2^20 states. Start with all dot cells set. Run BFS from that mask. For each of U, D, L, R in that order, map every set cell to its destination. A wall or boundary keeps the cell where it is. Landing on E drops the cell from the mask. The goal is mask 0. BFS gives the shortest string, and trying commands in U,D,L,R order gives the required tie-break. The pitfall is forgetting that blocked moves stay put, or tracking one start instead of the whole set. Another is skipping a visited array over masks, which blows up the runtime. Precompute each cell's destination per direction so each transition is cheap. Store parent pointers to rebuild the string.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Universal Exit Command Sequence cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Universal Exit Command Sequence FAQ
What's the core trick in Universal Exit Command Sequence?+
Make the set of possible current positions your BFS state, stored as a bitmask over the dot cells. Each command transforms the whole set at once. Cells that hit E vanish, and the goal is the empty set. BFS over masks gives the shortest string.
Why does the 20 dot cell limit matter?+
It signals bitmask state. Twenty cells means at most about a million masks, which is fine for BFS with four transitions each. Without that cap you couldn't enumerate subsets, so the constraint tells you the intended solution directly.
How do I get the tie-break right?+
Expand commands in U, D, L, R order at every BFS step and record the first parent that reaches each mask. Because BFS processes by length and you try directions in order, the first path found to the empty mask is the lexicographically ordered shortest one under that rule.
What's the most common bug on this one?+
Removing a cell when it bumps a wall. It must stay in the set at its current position. Only reaching E removes it. Also watch for two cells merging into one, since a bitmask handles that naturally by setting the same bit.
How do I prepare in 48 hours for a Google OA like this?+
Practice BFS over compressed states, especially bitmask sets. Write one transition function that maps a mask under a direction, and rebuild a path from parent pointers. Then test your code on both examples and a single-row grid to confirm the merge and bump behavior.