PPractick

DSA interview question

Ninja and His Friends (Maximum Chocolates Collection)

Medium · Algorithms · Topics: dynamic programming, 3d dp, matrix

The problem

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.

Function signature

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)

Test cases

Input gridnmOutputExplanation
[[3,1,1],[2,5,1],[1,5,5],[2,1,1]]4324Both alligators collect optimally moving down, sharing no overlapping cells beyond necessity
[[1,2,3],[4,5,6],[7,8,9]]3337Ninja from (0,0), friend from (0,2), best combined path
[[5]]115Both start and end on same single cell — counted once
[[1,1],[1,1]]224Small grid, straightforward paths
[[0,0,0],[0,0,0]]230No chocolates anywhere

(Always confirm: grid values are non-negative; both start on row 0 at columns 0 and m-1.)

How interviewers phrase it

Clarifying questions to ask

  1. Do both characters start on row 0, at columns 0 and m-1 respectively, and must reach row n-1? Confirm boundary conditions.
  2. On each move, can they go to 3 directions (left-diagonal, straight down, right-diagonal), or fewer?
  3. If both land on the same cell, is the chocolate counted once or can each collect it independently (double count)? (This problem: counted once.)
  4. Can characters move out of grid bounds — i.e., is movement restricted so column index stays within [0, m-1]?
  5. Are chocolate values always non-negative, or can there be negative values (penalties)?

Follow-ups to expect

  1. "Can you reduce space complexity?"
  2. "What if there were 3 collectors instead of 2?"
  3. "Cherry Pickup I: single robot goes from top-left to bottom-right and back to top-left, maximizing cherries, each cell usable once."
  4. "What if diagonal moves aren't allowed, only up/down/left/right?"
  5. "Can you reconstruct the actual paths, not just the max sum?"
  6. "What's the time and space complexity, and can it be improved further?"

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