Minimum-Weight Path Through a Matrix
Updated
Problem statement
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[][]) → intExamples
Example 1
grid = [[0,5],[2,1]]return = 1The 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 = 2Move 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 = -1All three neighboring cells of the start are blocked, so no path reaches the destination.
Constraints
1 <= grid.length <= 50and every row has exactlygrid.lengthcells.- Each cell is
-1or an integer from0through1000. - 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