FastPrepMinimum-Weight Top-to-Bottom Grid Path
Problems/Minimum-Weight Top-to-Bottom Grid Path

Minimum-Weight Top-to-Bottom Grid Path

Google logoGoogle● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report asks for the weighted shortest-path follow-up and mentions BFS versus Dijkstra. It does not say what cells cost, what blocks a cell, or what to do when the bottom is unreachable. Our version is about 85% the same as the reported question.

You are given a rectangular matrix weights. A value of -1 marks a blocked cell. Every other value is a positive traversal cost.

You may start at any unblocked cell in the top row. From an unblocked cell, you may move one cell up, down, left, or right, staying inside the matrix and never entering a blocked cell.

The cost of a path is the sum of the weights of every cell in the path, including its starting and ending cells. Return the minimum possible cost of reaching any unblocked cell in the bottom row. Return -1 if no such path exists.

Function

minimumTopToBottomPath(weights: int[][]) → long

Examples

Example 1

weights = [[1,50,50,50],[1,1,1,50],[50,50,1,50]]return = 5

The cheapest path is (0,0) -> (1,0) -> (1,1) -> (1,2) -> (2,2), with total cost 1 + 1 + 1 + 1 + 1 = 5. It uses more moves than the direct expensive routes.

Example 2

weights = [[5,1,5],[1,-1,1],[1,1,1]]return = 7

Starting at (0,0) and moving down twice costs 5 + 1 + 1 = 7. The low-cost top-middle cell cannot move down through the blocked center and needs a more expensive detour.

Example 3

weights = [[1,-1],[-1,-1]]return = -1

No unblocked bottom-row cell is reachable from the only unblocked top-row cell.

Constraints

  • 1 <= weights.length <= 500
  • 1 <= weights[i].length <= 500
  • Every row has the same length.
  • Every cell is -1 or an integer in [1, 10^6].
  • A move changes the row or column by exactly 1, but not both.

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

  • Shortest Travel Time in a Grid with Traffic LightsWaymo · PHONE SCREENWaymo interview questions
  • Shortest Path with Directional Move CostsThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
  • Minimum-Cost Path Through a Weighted GridVisa · OAVisa interview questions
  • Cookie Monster Minimum Arrival TimeIntuit · OAIntuit interview questions
  • Cheapest Flights Within K StopsAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Fire Station Response BillLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • Shortest Increasing Path to a TargetUber · ONSITE INTERVIEWUber interview questions
  • Path With Maximum ProbabilitySalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Find and Reconstruct a Binary-Matrix PathMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
  • Dijkstra's Shortest PathsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions

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
  • Google Experienced SWE First Round and Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public long minimumTopToBottomPath(int[][] weights) {
        // Write your code here.
    }
}
Checking account…