Grid Robot Route with Charging Priorities
Reported by candidates from Uber's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Uber reportedly asked this one in September 2026, and it looks like a simple grid walk until you read the priority rules. The solution hinges on a queue-driven breadth-first search, run from S, from each charger, and toward T, so you get exact shortest distances between the few special cells. With at most 8 chargers and a 10x10 grid, the real work is turning the grid into a tiny graph. If you blank on how to rank the three values, StealthCoder is the invisible backup running during the live assessment.
The problem
A robot travels through a rectangular grid from its unique S cell to its unique T cell. A cell is one of S, T,. (ordinary open space), C (a charging cell), or # (a wall). Each move goes up, down, left or right to a non-wall cell and consumes one unit of battery. There are no diagonal moves. The robot starts with a full battery of a capacity that you must determine. Whenever it enters a C cell, it automatically refills to that capacity and the number of charging visits increases by one. Refilling is mandatory on entry, even on a repeated visit. The move into a charger still costs one unit, but arriving with exactly zero battery is allowed before refilling. For a route, the required battery capacity is the largest number of moves in any segment from the start or a charging visit to the next charging visit or the target. The initial full battery is not a charging visit. Revisiting S does not refill the battery. The journey ends immediately on reaching T. Compare routes by the following priorities, in this order: Minimize the number of charging visits. Among routes with that minimum, minimize the required battery capacity. Among routes tied on both preceding values, minimize the total number of moves. Return these three optimal values as [chargingVisits, requiredCapacity, totalMoves]. Return [-1, -1, -1] if no route reaches the target. Return the values only, not the route's coordinates. Cells may be revisited, but every charger entry is counted; there is no free option to pass through a charger without refilling. Function optimalChargingCost(grid: String[]) → int[] Examples Example 1 grid = ["SCT","..."] return = [0,4,4] The direct two-move route enters the charger, giving [1,1,2]. Going around the bottom uses no charger and gives [0,4,4], which wins because charging visits have first priority. Example 2 grid = ["SC...",".###.",".###T",".C..."] return = [1,4,8] The top-and-right corridor takes six moves and visits the top charger after one move, so it needs capacity five. The left-and-bottom corridor takes eight moves and reaches its charger after four moves, giving two four-move segments. Both use one charger, so [1,4,8] beats [1,5,6]. Constraints The grid has from 1 through 10 rows and from 1 through 10 columns, with at least 2 cells in total. Every row has the same length and contains only S, T,., C and #. There is exactly one S and one T; they are distinct cells. There are at most 8 charging cells. The robot cannot leave the grid or enter walls.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to stop thinking about the grid and think about nodes: S, T and up to 8 chargers. Run BFS from each one to get shortest step counts to every other node, with walls blocking and chargers treated as endpoints only. A BFS leg must not pass through another C cell, because entry forces a refill and counts as a visit. Then search over the small graph with state (node, visits). For each visit count, you want the minimum bottleneck, which is the largest leg, and the minimum total moves. Compare tuples in the stated order. The common pitfall is letting BFS walk through chargers for free, which breaks the visit count. Another is treating the capacity as a sum instead of a max over segments. Revisiting chargers is legal but never helps minimize visits. If the graph logic slips mid-assessment, StealthCoder can supply the structure live.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Grid Robot Route with Charging Priorities 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Uber's OA.
Uber reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Grid Robot Route with Charging Priorities FAQ
What's the core trick in the Uber grid robot charging problem?+
Compress the grid into a graph of S, T and the chargers. Run BFS from each of those nodes to get shortest leg lengths, with other chargers blocked as pass-through cells. Then optimize over legs using the priority order of visits, capacity, total moves.
Why can't BFS just pass through a charger?+
Entering a C cell forces a refill and adds a charging visit. So any path through one is really two segments. Stop each BFS at chargers so every leg is a clean segment between special cells.
How is required capacity calculated?+
It's the maximum leg length across the route, not the sum. A leg runs from the start or a charger to the next charger or the target. The initial full battery doesn't count as a visit, and revisiting S doesn't refill.
How do I compare routes with three priorities?+
Use tuple comparison: (visits, capacity, total moves). Since capacity is a max and not additive, a plain Dijkstra won't work directly. Iterate by visit count, then track best (max leg, total) per node, or use a minimax then total comparison.
How should I prepare in 48 hours?+
Practice multi-source BFS on grids, then a small graph search over a handful of nodes. Write BFS distance code until it's automatic. Test the two examples by hand, especially the one where the no-charger detour beats the shorter charged path.