Code Room
CodingHardcod-g523
Subject Grid dpLevel Senior–Staff~40 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

On an n x n grid each cell holds 0, 1 (a cherry), or -1 (a thorn you cannot enter). Starting at (0,0), walk to (n-1,n-1) moving only right or down picking up cherries (setting them to 0), then walk back to (0,0) moving only left or up. Return the maximum cherries collected over both trips; if no valid round trip exists, return 0. Start and end cells are never thorns.

Implement
cherry_pickup_two_pass(grid: list[list[int]]) → int
Examples
in[[[0,1,-1],[1,0,-1],[1,1,1]]]out5
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.