FastPrepMinimum Distance to Return a Book
Problems/Minimum Distance to Return a Book

Minimum Distance to Return a Book

Google logoGoogle● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report gives the grid, several libraries and train stations, a BFS approach, and a formula for station shortcuts. It does not give how cells are encoded, the exact distance goal, how station costs line up, what to do when a place is unreachable, a function signature, or the input limits. Our version is about 88% the same as the reported question.

You are given a rectangular map grid, a starting position start, and a list stationToLibrary. You want to return a book to a library as cheaply as possible.

Each cell in grid is one of these:

  • . is an open cell.
  • # is a blocked cell.
  • L is a library.
  • T is a train station.

Each step moves you one cell up, down, left, or right and costs 1. You must stay inside the map and never step onto a blocked cell. You can walk through libraries and train stations.

Every train station has a direct train to its closest library. stationToLibrary gives that train distance for each T cell. The stations are listed in reading order: row by row from the top, and left to right within a row. Once you reach a station, you can take its train right away and finish the trip at that library. The train ride adds the station's train distance to your total.

A trip can go one of two ways:

  • Walk straight onto a library.
  • Walk to a train station, then take its train.

Return the smallest total distance needed to return the book. Return -1 if neither kind of trip is possible.

Function

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

Examples

Example 1

grid = ["...L",".##.","T..."]start = [0,0]stationToLibrary = [2]return = 3
grid
01230...L1.##.2T...

Walking right three times reaches the library at [0,3]. The other route walks two steps to the station and then rides distance 2, for a total of 4. Walking straight to the library is cheaper, so the answer is three.

Example 2

grid = ["L###","###T","...."]start = [2,0]stationToLibrary = [2]return = 6
grid
01230L###1###T2....

The library is walled off, so you cannot walk to it. The station at [1,3] is four walking steps away, and its train distance is 2. Four plus two gives an answer of six.

Example 3

grid = ["L#T","###","..."]start = [2,1]stationToLibrary = [3]return = -1
grid
0120L#T1###2...

The blocked middle row cuts the start off from both the library and the train station. Neither kind of trip is possible, so the answer is minus one.

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.

More Google Onsite Interview questions

  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
  • Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
  • Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Fire Station Response BillLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • Shortest Increasing Path to a TargetUber · ONSITE INTERVIEWUber interview questions
  • Find and Reconstruct a Binary-Matrix PathMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
  • Shortest Travel Time in a Grid with Traffic LightsWaymo · PHONE SCREENWaymo interview questions
  • Grid ExplorationIMC Trading · OAIMC Trading interview questions
  • Drone Delivery with Battery Swap StationsCisco · OACisco interview questions
  • Number of IslandsAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Nearest Charging Station on a GridSpotify · ONSITE INTERVIEW
  • Island Count and Maximum AreaOracle · ONSITE INTERVIEWOracle interview questions
  • Minimum Friend API CallsMercor · ONSITE INTERVIEWMercor interview questions

Interview reports that mention this question

  • Google Intern Prefix Sums and Heaps: 6 Experiences (Part 3 of 8)

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int minimumLibraryReturnDistance(String[] grid, int[] start, int[] stationToLibrary) {
      // Write your code here.
    }
}
Checking account…