Code RoomCherry collection round trip
HardPrep Room Coding #1090

Cherry collection round trip

CodingAlgorithms & data structuresSenior–Staff~40 min

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.

0:00 of about 40 min
InputExpectedGot
[[[0,1,-1],[1,0,-1],[1,1,1]]]5not run yetsample