Simulate Orchard Rot Spread
Updated
Problem statement
Given a 2D grid orchard and an integer days, simulate how rot spreads through the orchard for days days and return the state of the orchard after the last day.
Each cell of orchard holds one character:
'-': an empty cell.'T': a healthy tree.'R': a rotten tree.
Each day, rot spreads from every rotten tree to its adjacent cells (up, down, left and right), so every healthy tree next to a rotten tree becomes rotten. Empty cells stay empty. A tree that becomes rotten on a day starts spreading rot on the next day.
Note: You don't need the most optimal solution. Any solution with time complexity no worse than O(days * rows * cols) fits within the time limit, where rows and cols are the number of rows and columns in orchard.
Function
simulateOrchard(orchard: char[][], days: int) → char[][]Examples
Example 1
orchard = [["T","R","-","-"],["-","T","T","R"],["T","T","-","R"],["-","-","R","-"]]days = 2return = [["R","R","-","-"],["-","R","R","R"],["T","R","-","R"],["-","-","R","-"]]Cells are written as (row, column).
- Day 1: the rotten trees at
(0, 1)and(1, 3)rot the healthy trees at(0, 0),(1, 1)and(1, 2). - Day 2: the tree at
(1, 1), rotten since day 1, rots the tree at(2, 1). The tree at(2, 0)stays healthy, because(2, 1)only became rotten on day 2.
Example 2
orchard = [["R","T","-","T"],["-","T","-","T"]]days = 3return = [["R","R","-","T"],["-","R","-","T"]]Rot reaches (0, 1) on day 1 and (1, 1) on day 2, then has no healthy tree left to reach. Empty cells cut off the two trees in the last column, so they stay healthy.
Example 3
orchard = [["T","R"],["T","T"]]days = 0return = [["T","R"],["T","T"]]With days = 0, no day passes, so the orchard is returned unchanged.
Constraints
1 <= orchard.length <= 501 <= orchard[i].length <= 50- All rows of
orchardhave the same length. orchard[i][j]is'-','T', or'R'.0 <= days <= 1000
More Capital One Online Assessment questions
- Count Pairs of Overlapping SquaresOA · Seen Nov 2026
- Distinct Values on Maximum-Sum FramesOA · Seen Nov 2026
- Lowest Number in an Open RangeOA · Seen Nov 2026
- Branch with the Most FilesOA · Seen Nov 2026
- Earliest Meeting Time for All EmployeesOA · Seen Nov 2026
- Earn the Most 🐝OA · Seen Nov 2026
- Rank Football TeamsOA · Seen Oct 2026
- Count Subarrays Matching a Comparison PatternOA · Seen Oct 2026
- Arithmetic Triplets After QueriesOA · Seen Oct 2026
- Count One-Swap Number PairsOA · Seen Oct 2026
Similar questions at other companies
- Simulate Orchard Rot SpreadAirbnb · OAAirbnb interview questions
- Simulate Orchard Rot SpreadZipRecruiter · OAZipRecruiter interview questions
- Competitive ForagingAkuna Capital · OAAkuna Capital interview questions
- Explore an Unknown Grid and Find a PathLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Grid Infection with Recovery After D DaysOpenAI · PHONE SCREENOpenAI interview questions
- Days Until Poisoned Plants StabilizeApple · PHONE SCREENApple interview questions
- Process Minesweeper Game OperationsAmperity · PHONE SCREENAmperity interview questions
- Minesweeper Board UpdateUber · ONSITE INTERVIEWUber interview questions
- Xiangqi Horse ReachabilityeBay · PHONE SCREENeBay interview questions
- Minesweeper Board UpdateClay · ONSITE INTERVIEW
Capital One interview experiences
- Capital One Coding OA Questions: 3 Experiences
- Capital One Experienced Engineer Power Day and OA: 6 Experiences
- Capital One New Grad OA Questions: 14 Experiences (Part 2 of 2)
- Capital One TDP Agentic Round (AI Coding): 7 Experiences
- Capital One SWE Power Day, Level Not Reported: 6 Experiences
- Capital One TIP Intern OA and Power Day: 13 Experiences