DSA interview question
You're simulating a physics-based puzzle game where a ball drops from the top row of an n x n grid of "energy cost" tiles. From any tile in a row, the ball can fall straight down, or diagonally to the immediately-left or immediately-right column in the row below (never wrapping around edges). The ball starts at any tile in the top row and falls all the way to the bottom row. Your task: compute the minimum total energy cost the ball accumulates over any such falling path.
Example: matrix
2 1 3
6 5 4
7 8 9
Minimum falling path: 1 → 4 → 7 = 12.
int minFallingPathSum(vector<vector<int>>& matrix);
Input matrix | Output | Explanation |
|---|---|---|
[[2,1,3],[6,5,4],[7,8,9]] | 13 | Path: 1 → 4 → 8 (verify: 1+4+8=13) |
[[-19,57],[-40,-5]] | -59 | Path: -19 → -40 |
[[1]] | 1 | Single cell, trivially the answer |
[[1,2],[3,4]] | 4 | Path: 1 → 3 |
[[100,100,100],[100,1,100],[100,100,100]] | 101 | Path: 100 → 1 → 100 (forced diagonal-diagonal through center) |
[[5,5],[5,5]] | 10 | Any path gives the same sum |
(Confirm: matrix is always square n x n, n >= 1; values can be negative.)
(row, col): is it (row+1, col-1), (row+1, col), (row+1, col+1) — i.e., straight down or one step diagonally in either direction?n x n), or could it be rectangular (which would change nothing structurally, but worth confirming)?On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in