Maximum Grid Path Sum in Exactly N Moves
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The start cell is worth zero, and every cell you step into pays out its value, even if you've already been there. That one detail drives this Bloomberg OA, reported in November 2020. You get exactly N moves on a grid, and you want the biggest total. The hinted pattern is breadth-first search, but the real engine is layered DP over steps. If you're taking this in the next day or two, learn the state first. If the state blanks on you mid-assessment, StealthCoder sits invisibly on your screen as a safety net and reads the problem for you.
The problem
Start at [startRow,startCol]. Make exactly moves one-cell moves up, down, left, or right without leaving the grid. The start contributes zero; each entered cell contributes its value. Revisiting is allowed. Return the maximum achievable sum. Function maximumPathSum(grid: int[][], startRow: int, startCol: int, moves: int) → int Examples Example 1 grid = [[2,3,4,6],[1,2,3,5],[3,4,0,5],[0,1,2,3]] startRow = 2 startCol = 2 moves = 2 return = 10 Move right to 5 and back or between the two adjacent 5 cells for a total of 10. Constraints The grid is nonempty and rectangular. 0 <= moves <= 1000. A valid move sequence exists.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that revisiting is allowed, so you never need a visited set. Define dp[k][r][c] as the best sum after k moves ending at (r,c). Start with dp[0][startRow][startCol] = 0 and everything else at negative infinity. For each step, a cell takes the max of its four neighbors from the previous layer, plus its own value. Answer is the max over the final layer. That's a BFS by levels, but you keep the best score per cell instead of a boolean. Only two layers are needed in memory. Time is moves times rows times cols. The common pitfall is marking cells visited and killing the bounce-back strategy from Example 1, where you oscillate between two 5s for 10. Another miss is forgetting unreachable cells must stay at negative infinity, not zero. If the recurrence slips under pressure, StealthCoder can hand you the layered DP during the live OA.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Maximum Grid Path Sum in Exactly N Moves 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Maximum Grid Path Sum in Exactly N Moves FAQ
What's the trick in Maximum Grid Path Sum in Exactly N Moves?+
Revisiting is allowed, so skip the visited set. Run a DP by move count where each cell stores the best sum reachable in exactly k moves. Each layer takes the max of four neighbors plus the cell's value. Oscillating between two high cells is often optimal.
Is this really BFS or is it DP?+
It's both in shape. You expand level by level like BFS, one level per move. But you keep the max score per cell per level rather than a reached flag. That makes it DP over steps. Plain BFS with a visited set gives wrong answers here.
What's the time and space complexity?+
Time is O(moves * rows * cols), since each layer touches every cell and checks four neighbors. Space drops to O(rows * cols) with two rolling layers. With moves up to 1000, that's fine for typical grid sizes.
What mistakes sink people on this one?+
Three common ones. Using a visited set. Initializing unreachable cells to 0 instead of negative infinity, which lets you teleport. Adding the start cell's value, when the statement says the start contributes zero. Check Example 1 returns 10 before submitting.
How do I prepare for this Bloomberg OA in 48 hours?+
Write the rolling-layer grid DP from scratch once, then test moves = 0 and a 1x1 grid. Reported in November 2020, the shape still shows up in step-count grid problems. Practice the recurrence until you can type it without thinking.