FastPrepMinimum-Weight Path Through a Matrix
Problems/Minimum-Weight Path Through a Matrix

Minimum-Weight Path Through a Matrix

Meta logoMeta● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Meta onsite interview. The weighted follow-up makes each value in the matrix the cost of traveling through it. The report did not say how blocked cells are marked, how the starting cell counts, or the exact limits. Our version is about 70% the same as the reported question.

Given a square matrix grid, find the minimum travel cost from its top-left cell to its bottom-right cell. A cell with value -1 is blocked. Every other value is the nonnegative cost of entering that cell.

You may move to any of the eight neighboring cells, including diagonals. The starting cell is not charged; every later entered cell contributes its value. Return the minimum total cost, or -1 if the destination cannot be reached.

Function

minimumWeightedPathCost(grid: int[][]) → int

Examples

Example 1

grid = [[0,5],[2,1]]return = 1
grid
01005121

The diagonal enters the destination at cost 1; the starting cell is free.

Example 2

grid = [[0,9,9],[1,-1,1],[2,1,0]]return = 2
grid
012009911-112210

Move through the cost-1 cell at (1,0), then the cost-1 cell at (2,1), then the cost-0 destination.

Example 3

grid = [[0,-1,2],[-1,-1,1],[2,1,0]]return = -1
grid
01200-121-1-112210

All three neighboring cells of the start are blocked, so no path reaches the destination.

Constraints

  • 1 <= grid.length <= 50 and every row has exactly grid.length cells.
  • Each cell is -1 or an integer from 0 through 1000.
  • Costs are nonnegative, so revisiting a cell cannot improve a minimum-cost path.

More Meta Onsite Interview questions

  • Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Run-Length Encoded Vector Dot ProductPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Deterministic K-Means AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
  • Linear Layer Forward and Backward PassesONSITE INTERVIEW · Seen Sep 2026
  • Reservoir Sampling with Recorded DrawsONSITE INTERVIEW · Seen Sep 2026
  • Stochastic Gradient Descent with MomentumONSITE INTERVIEW · Seen Sep 2026
  • Top K Largest Values with a HeapONSITE INTERVIEW · Seen Sep 2026
  • Partition a Card Deck into FifteensONSITE INTERVIEW · Seen Sep 2026
  • Insert into a Sorted Circular ListONSITE INTERVIEW · Seen Aug 2026

Similar questions at other companies

  • Cheapest Flights Within K StopsAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Minimum Product Path in a Directed GraphGoogle · ONSITE INTERVIEWGoogle interview questions
  • Path With Maximum ProbabilitySalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Dijkstra's Shortest PathsMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Single-Source Shortest PathsRippling · ONSITE INTERVIEWRippling interview questions
  • Hub Vertices Within a RadiusThe Walt Disney Company · ONSITE INTERVIEWThe Walt Disney Company interview questions
  • Minimum Path Cost with Edge DiscountsSkyscanner · ONSITE INTERVIEWSkyscanner interview questions
  • Shortest Travel Time in a Grid with Traffic LightsWaymo · PHONE SCREENWaymo interview questions
  • Shortest Paths in a Nonnegative Directed GraphNation with NaMo · PHONE SCREEN
  • Dijkstra Shortest Paths in a Weighted Undirected GraphGoldman Sachs · PHONE SCREENGoldman Sachs interview questions

Meta interview experiences

  • Meta PhD SWE Intern AI Coding Phone Screen: 1 Experience
  • Meta Business Support Engineer Phone Screen: 1 Experience
  • Meta Software Engineer Onsite Questions: 3 Experiences
  • Meta AI Native Engineer OA and Onsite: 1 Experience
  • Meta AI-Native Coding Interview: 3 Experiences
  • Meta Production Engineer Intern Questions: 6 Experiences
All Meta interview questionsSee Meta hiring insights
Saved
class Solution {
    public int minimumWeightedPathCost(int[][] grid) {
      // Return the minimum sum of entered cell weights.
    }

}
Checking account…