Reported August 2026
Googlebreadth first search

Minimum Distance to Return a Book

Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Google OA. Under 2s to a working solution.
Founder's read

Google, reported August 2026. The grid can hold up to 200000 cells, so anything that reruns a search from every library or every station is dead on arrival. That's the whole point of this one. It's a plain shortest-path-on-a-grid problem wearing a train-station costume, and the pattern is breadth-first search from the start cell. If you've got the OA invite and 48 hours, this is one to recognize on sight. StealthCoder sits as a safety net during the live assessment if your mind goes blank on the setup, but the idea is short enough to hold in your head.

The problem

You are given a rectangular map grid, a starting coordinate start, and the direct train distance from every train station to its closest library.
Each cell in grid is one of:
., an open cell;
#, a blocked cell;
L, a library; or
T, a train station.
You may walk one cell up, down, left, or right at a cost of 1, staying inside the map and never entering a blocked cell. Libraries and train stations are walkable.
The values in stationToLibrary correspond to the T cells in row-major order. Reaching a train station lets you immediately finish the trip at that station's closest library for the corresponding additional train distance.
Return the minimum total distance needed to return the book. A valid trip either walks directly onto a library or walks to a train station and then uses its direct connection. Return -1 when neither option is reachable.

Function
minimumLibraryReturnDistance(grid: String[], start: int[], stationToLibrary: int[]) → int

Examples
Example 1
grid = ["...L",".##.","T..."]
start = [0,0]
stationToLibrary = [2]
return = 3
Walking right three times reaches the library at [0,3]. Walking two steps to the station and then traveling distance 2 would cost 4.
Example 2
grid = ["L###","###T","...."]
start = [2,0]
stationToLibrary = [2]
return = 6
The library cannot be reached by walking. The station at [1,3] is four walking steps away, and its direct train distance is 2.
Example 3
grid = ["L#T","###","..."]
start = [2,1]
stationToLibrary = [3]
return = -1
The blocked middle row separates the start from both the library and the train station.

Constraints
1 <= grid.length and 1 <= grid[row].length.
All rows have the same length, and the map contains at most 200000 cells.
Every cell is one of., #, L, or T.
The map contains at least one library.
start is an in-bounds coordinate whose cell is not blocked.
stationToLibrary.length equals the number of train stations.
0 <= stationToLibrary[i] <= 10^9.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Run one BFS from start. Every step costs 1, so BFS gives exact walking distance to every reachable cell. No Dijkstra needed. Then take the minimum over two candidates: the walking distance to any reachable L cell, and for each reachable T cell, walking distance plus its stationToLibrary value. Map T cells to the array by counting them in row-major order as you scan the grid. The common pitfall is trying to search from libraries or pairing stations with libraries yourself. You don't need to, since the train distance is given. Another trap is overflow: distances plus 10^9 can exceed int range in some languages, so use a 64-bit value. Also remember the start cell itself might be an L or a T. Return -1 if neither candidate exists. Total work is O(cells). If you freeze live, StealthCoder is the hedge, but this is one BFS plus a min.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Minimum Distance to Return a Book 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 StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Google's OA.

Google 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.

Minimum Distance to Return a Book FAQ

How hard is Minimum Distance to Return a Book really?+

Easy to medium. It's a single-source BFS on a grid with one twist at the end. If you've written grid BFS before, the code is about 30 lines. The difficulty is spotting that one BFS covers both options.

What's the trick?+

Don't search per library or per station. Run one BFS from start, then combine results: min of walking distance to any L, and walking distance to each T plus its train distance. Unit step costs mean plain BFS is enough.

Why not use Dijkstra?+

Every walking move costs 1, so BFS already finds shortest paths. The train costs are only added after the walk, at the end, so they never enter the search. Dijkstra works but adds a heap and log factor for nothing.

What edge cases break solutions?+

The start being on an L or T cell, unreachable libraries with a reachable station, nothing reachable (return -1), and overflow when adding up to 10^9 to a walking distance. Also indexing stationToLibrary in row-major order, not by discovery order in BFS.

How do I prepare for this in 48 hours?+

Write grid BFS from memory twice: queue, visited array, four directions, distance array. Then practice a variant where you scan the grid first to index special cells. Check the 200000-cell limit means O(cells) is required.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Google.

OA at Google?
Invisible during screen share
Get it