PPractick

DSA interview question

Minimum Falling Path Sum

Medium · Algorithms · Topics: dynamic programming, matrix

The problem

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.

Function signature

int minFallingPathSum(vector<vector<int>>& matrix);

Test cases

Input matrixOutputExplanation
[[2,1,3],[6,5,4],[7,8,9]]13Path: 1 → 4 → 8 (verify: 1+4+8=13)
[[-19,57],[-40,-5]]-59Path: -19 → -40
[[1]]1Single cell, trivially the answer
[[1,2],[3,4]]4Path: 1 → 3
[[100,100,100],[100,1,100],[100,100,100]]101Path: 100 → 1 → 100 (forced diagonal-diagonal through center)
[[5,5],[5,5]]10Any path gives the same sum

(Confirm: matrix is always square n x n, n >= 1; values can be negative.)

How interviewers phrase it

Clarifying questions to ask

  1. Confirm the exact set of allowed moves from (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?
  2. Can the ball start at any column in the top row, not just column 0?
  3. Does the path need to reach a specific target in the bottom row, or can it end at any column?
  4. Is the matrix guaranteed to be square (n x n), or could it be rectangular (which would change nothing structurally, but worth confirming)?
  5. Can matrix values be negative? (Confirms DP is required — no greedy shortcuts possible.)

Follow-ups to expect

  1. "Can you solve it in O(1) extra space (in-place)?"
  2. "What if you also need to output the actual path, not just the sum?"
  3. "How does this differ from the Triangle problem?"
  4. "Can you do this top-down with memoization instead of bottom-up?"
  5. "What if diagonal moves could skip a column (e.g., ±2)?"
  6. "What if the ball can move in any of 8 directions instead of only downward?"

Practise questions like this with a real engineer

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 interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in