FastPrepCherry Pickup
Problems/Cherry Pickup

Cherry Pickup

Infosys logoInfosys● HardFULLTIMEOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Infosys online assessment. One of the three coding questions was a grid dynamic programming task with two walkers. The candidate named it as the well-known Cherry Pickup problem and solved all of its tests.

No statement, limits, or examples were shared, so this version follows the usual Cherry Pickup rules: a walk down and right, a walk back up and left, thorns block cells, a cherry counts once, and the answer is 0 when the bottom-right cell cannot be reached.

Our version is about 90% the same as the reported question.

You are given an n x n grid. Each cell holds one of three values:

  • 0: an empty cell you can walk through,
  • 1: a cell with one cherry, which you can also walk through,
  • -1: a thorn that blocks the cell.

You start at the top-left cell (0, 0). First walk to the bottom-right cell (n - 1, n - 1), moving only right or down. Then walk back to (0, 0), moving only left or up. You may never step on a thorn.

Whenever you stand on a cell with a cherry, including the start cell (0, 0), you pick it up and the cell becomes empty. So a cherry counts only once, even if both walks pass through its cell.

Return the largest number of cherries you can collect. If there is no path from (0, 0) to (n - 1, n - 1) that avoids the thorns, return 0.

Function

cherryPickup(grid: int[][]) → int

Examples

Example 1

grid = [[0,1,1],[1,-1,0],[1,1,0]]return = 5
grid
012001111-102110

Walk along the top row and down the right column: (0, 0) → (0, 1) → (0, 2) → (1, 2) → (2, 2). This picks the cherries at (0, 1) and (0, 2). Walk back along the bottom row and up the left column: (2, 2) → (2, 1) → (2, 0) → (1, 0) → (0, 0). This picks the cherries at (2, 1), (2, 0), and (1, 0). All 5 cherries are collected.

Example 2

grid = [[1,1,0],[1,1,1],[1,0,1]]return = 7
grid
012011011112101

The grid has 7 cherries. Go (0, 0) → (0, 1) → (1, 1) → (1, 2) → (2, 2) to pick 5 of them, then come back (2, 2) → (2, 1) → (2, 0) → (1, 0) → (0, 0) to pick the cherries at (2, 0) and (1, 0). Taking the best single walk first does not always work: the walk (0, 0) → (1, 0) → (1, 1) → (1, 2) → (2, 2) also picks 5, but then no way back can reach both (0, 1) and (2, 0), so that plan ends with only 6.

Example 3

grid = [[1,-1],[-1,1]]return = 0
grid
0101-11-11

From (0, 0) the only moves are to (0, 1) or (1, 0), and both are thorns. The bottom-right cell cannot be reached, so the answer is 0.

Constraints

  • n == grid.length == grid[i].length
  • 1 <= n <= 50
  • grid[i][j] is -1, 0, or 1
  • grid[0][0] != -1 and grid[n - 1][n - 1] != -1

More Infosys Online Assessment questions

  • Lowest Common Ancestor with a Changing RootOA · Seen Nov 2026
  • Minimum Leaf Path Sum over All RootsOA · Seen Nov 2026
  • Split Array to Minimize the Largest SumOA · Seen Nov 2026
  • Lexicographically Smallest Grid Path with One Letter ChangeOA · Seen Nov 2026
  • Minimum Cost to Supply Water with Wells and PipesOA · Seen Nov 2026
  • Minimum Cost to Tile a Strip with Widths 1, 2, and 3OA · Seen Nov 2026
  • Minimum Rounds to Complete All TasksOA · Seen Nov 2026
  • Count Equal-Distinct Character PartitionsOA · Seen Oct 2026
  • Maximum Alternating-Index Packet ValueOA · Seen Oct 2026
  • Usable Water with Contaminated PillarsOA · Seen Oct 2026

Similar questions at other companies

  • Maximum Sum RectangleDeloitte · OADeloitte interview questions
  • Largest Square of OnesOpenAI · OAOpenAI interview questions
  • Largest Plus SignxAI · OAxAI interview questions
  • Dungeon GameAmazon · ONSITE INTERVIEW
  • Longest Increasing Path in a MatrixNew Relic · PHONE SCREEN · ONSITE INTERVIEW
  • Longest Increasing Path in a MatrixVisa · ONSITE INTERVIEWVisa interview questions
  • Maximal Rectangle in a Binary MatrixGoogle · ONSITE INTERVIEWGoogle interview questions
  • Find Robots by Blocker DistancesUber · PHONE SCREENUber interview questions
  • Factory Cost — Minimum Cost Across StagesStripe · PHONE SCREENStripe interview questions
  • Maximum All-Ones Square Side LengthOmnissa · ONSITE INTERVIEWOmnissa interview questions
All Infosys interview questionsSee Infosys hiring insights
Saved
class Solution {
    public int cherryPickup(int[][] grid) {
        // Write your code here.
    }
}
Checking account…