Minimum Distance to Return a Book
Updated
Problem statement
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.Lis a library.Tis 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[]) → intExamples
Example 1
grid = ["...L",".##.","T..."]start = [0,0]stationToLibrary = [2]return = 3Walking 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 = 6The 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 = -1The 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.lengthand1 <= grid[row].length.- All rows have the same length, and the map contains at most
200000cells. - Every cell is one of
.,#,L, orT. - The map contains at least one library.
startis an in-bounds coordinate whose cell is not blocked.stationToLibrary.lengthequals 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