DSA interview question
You're building a route-optimization feature for a delivery-cost estimator. The delivery zones are arranged in a triangular pyramid layout — each level has one more zone than the level above it. Starting from the top zone, a package can move down to one of the two zones directly below it (adjacent) at each step, until it reaches the bottom level. Each zone has a delivery cost. Your task: find the minimum total cost to route the package from the top zone to any zone in the bottom row.
Example: given the triangle
2
3 4
6 5 7
4 1 8 3
the minimum path is 2 → 3 → 5 → 1 = 11.
int minimumTotal(vector<vector<int>>& triangle);
Input triangle | Output | Explanation |
|---|---|---|
[[2],[3,4],[6,5,7],[4,1,8,3]] | 11 | Path: 2 → 3 → 5 → 1 |
[[-10]] | -10 | Single row, trivially the answer |
[[1],[2,3]] | 3 | Path: 1 → 2 |
[[1],[-2,-3]] | -2 | Path: 1 → -2 (negative numbers allowed) |
[[1],[2,3],[4,5,6]] | 7 | Path: 1 → 2 → 4 |
[[5],[9,6],[4,6,8],[0,7,1,5]] | 15 | Path: 5 → 6 → 4 → 0 |
(Confirm: triangle can have negative numbers; the number of rows is at least 1, and row i has exactly i+1 elements.)
triangle[i][j], can I move to triangle[i+1][j] and triangle[i+1][j+1]? Confirm this precisely, since off-by-one errors are common.i guaranteed to have exactly i+1 elements (well-formed)?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