DSA interview question
You're designing a mini-game feature for a mobile app. Two characters — Ninja and his friend — start at the top-left and top-right corners of a grid-shaped level filled with chocolates. Both move simultaneously, row by row, and on each row they can move to the cell diagonally left, straight down, or diagonally right. If both characters land on the same cell, only one of them gets to pick up the chocolate there (no double counting). Your task: compute the maximum total chocolates both characters can collect together by the time they reach the bottom row.
Example: a 3x3 grid where Ninja starts at (0,0) and friend starts at (0,2), both moving down to row 2 — figure out the best paths to maximize combined pickup.
int maxChocolates(int n, int m, vector<vector<int>>& grid);
// n = number of rows, m = number of columns
// grid[i][j] = number of chocolates at cell (i, j)
Input grid | n | m | Output | Explanation |
|---|---|---|---|---|
[[3,1,1],[2,5,1],[1,5,5],[2,1,1]] | 4 | 3 | 24 | Both alligators collect optimally moving down, sharing no overlapping cells beyond necessity |
[[1,2,3],[4,5,6],[7,8,9]] | 3 | 3 | 37 | Ninja from (0,0), friend from (0,2), best combined path |
[[5]] | 1 | 1 | 5 | Both start and end on same single cell — counted once |
[[1,1],[1,1]] | 2 | 2 | 4 | Small grid, straightforward paths |
[[0,0,0],[0,0,0]] | 2 | 3 | 0 | No chocolates anywhere |
(Always confirm: grid values are non-negative; both start on row 0 at columns 0 and m-1.)
0 and m-1 respectively, and must reach row n-1? Confirm boundary conditions.[0, m-1]?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