FastPrepMinimum-Cost Path Through a Weighted Grid
Problems/Minimum-Cost Path Through a Weighted Grid

Minimum-Cost Path Through a Weighted Grid

Visa logoVisa● MediumNEW GRADOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The original report specifies a weighted grid with obstacles and a minimum-cost path. Our version fixes the endpoints at the corners, four-direction movement, a cost that includes both endpoints, a result for an unreachable end, examples, and limits.

You are given a rectangular integer grid. A value of -1 marks a blocked cell; every other value is the nonnegative cost of entering that cell.

Start at the top-left cell and reach the bottom-right cell using only up, down, left, or right moves. Return the minimum total cost, including the costs of both the start and destination cells. Return -1 if either endpoint is blocked or no valid path exists.

Function

minimumWeightedGridPathCost(grid: int[][]) → long

Examples

Example 1

grid = [[1,3,1],[1,5,1],[4,2,1]]return = 7
grid
012013111512421

One minimum route has costs 1, 3, 1, 1, and 1 for a total of 7.

Example 2

grid = [[1,-1],[-1,2]]return = -1
grid
0101-11-12

The destination is open, but both possible first moves are blocked.

Example 3

grid = [[9]]return = 9
grid
009

The single cell is both endpoints, so its weight is the complete path cost.

Constraints

  • 1 <= rows, columns <= 500.
  • The grid is rectangular and contains at most 200000 cells.
  • Each value is -1 or lies in [0, 1000000].
  • Every reachable path cost fits a signed 64-bit integer.

More Visa Online Assessment questions

  • Count Prime Values in SubtreesOA · Seen Nov 2026
  • Lock CodeOA · Seen Nov 2026
  • Minimum-Sum Tree Fill with Unit StepsOA · Seen Nov 2026
  • Longest Non-Decreasing Substring After Pair SwapsOA · Seen Oct 2026
  • Count Attack Vectors by Divisor RangeOA · Seen Oct 2026
  • Minimum Operations to Reduce a Transaction LedgerOA · Seen Oct 2026
  • Count Valid Space-Delimited WordsOA · Seen Oct 2026
  • Earliest Common Meeting StartOA · Seen Oct 2026
  • Maximum Index-Weighted Sum with Disjoint SwapsOA · Seen Oct 2026
  • Maximum Equalized Substrings After One Adjacent SwapOA · Seen Sep 2026

Similar questions at other companies

  • Cookie Monster Minimum Arrival TimeIntuit · OAIntuit interview questions
  • 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-Weight Top-to-Bottom Grid PathGoogle · ONSITE INTERVIEWGoogle interview questions
  • Minimum Stress PathWalmart · OAWalmart interview questions
  • Minimize Path ValuePalantir · OAPalantir interview questions
  • Grid ExplorationIMC Trading · OAIMC Trading interview questions
  • Minimize Path ValueSalesforce · OASalesforce interview questions
  • Minimum Wizard Referral CostAirbnb · OAAirbnb interview questions
  • Drone Delivery with Battery Swap StationsCisco · OACisco interview questions
All Visa interview questionsSee Visa hiring insights
Saved
class Solution {
    public long minimumWeightedGridPathCost(int[][] grid) {
      // write your code here
    }
}
Checking account…