Minimum Path Sum With Grid Switches
Reported by candidates from Infosys's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Infosys reportedly served this one in May 2026, and the edge case is what separates a pass from a fail. It looks like a plain grid path sum, but you can hop between two grids at any cell for a fee. The hinted pattern is BFS, but the clean answer is a two-layer dynamic programming table over the grid. If you've got an OA invite and 48 hours, learn this shape now. StealthCoder sits invisibly on your screen during the live assessment as a safety net if you blank on the state definition.
The problem
You are given two integer grids grid1 and grid2 with the same dimensions, and an integer switchCost. You need to travel from the top-left cell (0, 0) to the bottom-right cell. From a cell, you may move only one cell to the right or one cell down. You may use either grid for the path. At any cell, you may switch from the current grid to the other grid at the same row and column by paying switchCost. Switching does not add the current cell's value a second time; it only changes which grid is used for subsequent moves. Return the minimum possible path sum, including all visited cell values and any switch costs paid. Function minPathSumWithGridSwitches(grid1: int[][], grid2: int[][], switchCost: int) → int Examples Example 1 grid1 = [[1, 3], [4, 2]] grid2 = [[2, 1], [1, 5]] switchCost = 3 return = 6 Stay on grid1 and take the path (0,0) -> (0,1) -> (1,1), with sum 1 + 3 + 2 = 6. Example 2 grid1 = [[1, 100, 100], [1, 100, 1]] grid2 = [[100, 1, 1], [100, 1, 1]] switchCost = 2 return = 6 Start on grid1, move down to (1,0), switch to grid2, then move right twice. The cost is 1 + 1 + 2 + 1 + 1 = 6. Constraints grid1 and grid2 are non-empty grids with the same dimensions. The grids contain integer path costs. switchCost is an integer cost for changing grids at a cell.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is state. Each cell needs two values: the best cost to reach it while standing on grid1, and the best cost while standing on grid2. Call them dp[i][j][0] and dp[i][j][1]. Transition from the top or left neighbor on the same grid, then add the current cell's value from that grid. Switching at a cell means taking min(dp[i][j][0], dp[i][j][1] + switchCost) for layer 0, and the mirror for layer 1. The pitfall: adding the cell value twice when you switch. The statement says switching doesn't re-add it. Another trap is switching only at the start or end, or allowing just one switch. Multiple switches are legal. Also handle the first row and column where only one neighbor exists. Answer is the min of both layers at the bottom-right. That's O(rows*cols) time. If the state logic slips mid-assessment, StealthCoder can hand you the working table.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Minimum Path Sum With Grid Switches cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Infosys's OA.
Infosys reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Minimum Path Sum With Grid Switches FAQ
What's the trick in Minimum Path Sum With Grid Switches?+
Track two states per cell, one for each grid you're currently on. Compute arrival cost from the top or left, add the cell value, then relax each layer with the other layer plus switchCost. It's a standard grid DP with a layer dimension.
Do I need BFS or is DP enough?+
DP is enough and cleaner. Moves are only right or down, so the graph is acyclic and processing cells in row-major order works. BFS or Dijkstra would also be correct but adds overhead you don't need.
What's the edge case that breaks naive solutions?+
Double-counting the cell value on a switch, and allowing only one switch. Switching costs switchCost and changes the active grid without adding the cell again. Multiple switches along a path are allowed, so relax both layers at every cell.
Can I switch at the start or end cell?+
Per the statement, you may switch at any cell, including the first. Starting on grid2 costs grid2[0][0] with no fee, and starting on grid1 then switching costs the fee. Seed both layers, relaxing each against the other.
How do I prepare for this in 48 hours?+
Write classic minimum path sum from memory, then add the layer dimension. Test on Example 2, which should return 6. Practice the first row and column boundaries. If the assessment still catches you off guard, a hedge helps.