Plant Connected Vegetable Groups
Updated
Problem statement
Fill a rectangular garden with vegetable types. Type i + 1 needs exactly counts[i] cells. Cells of each planted type must form one group connected by up, down, left or right moves.
Build the grid this way, so every solution gives the same answer: visit row 0 from left to right, row 1 from right to left, then alternate direction on each row. Assign the first counts[0] visited cells to type 1, the next counts[1] to type 2, and continue in type order. Skip zero counts.
Return the resulting integer matrix. If the total count is zero, return an empty matrix, including when counts is empty or either dimension is zero. Otherwise the counts fill the rectangle exactly. Do not change counts.
Function
plantVegetables(rows: int, cols: int, counts: int[]) → int[][]Examples
Example 1
rows = 2cols = 3counts = [2,3,1]return = [[1,1,2],[3,2,2]]The snake visits (0,0), (0,1), (0,2), (1,2), (1,1), (1,0). The first 2 cells are type 1, the next 3 are type 2, and the last is type 3.
Example 2
rows = 2cols = 3counts = [0,0]return = []All counts are zero, so the result is empty even though the requested dimensions are positive.
Constraints
- 0 <= rows, cols <= 50
- 0 <= counts.length <= 2500
- 0 <= counts[i] <= 2500
- The total count is either 0 or rows * cols.
More Apple Onsite Interview questions
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Topological OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- FIFO Mutex Operation TraceONSITE INTERVIEW · Seen Oct 2026
- Implement Hit CounterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Parse Nested HTML into a TreeONSITE INTERVIEW · Seen Oct 2026
- Thread-Safe Concurrent Linked ListONSITE INTERVIEW · Seen Sep 2026
- Palindrome Match PercentageONSITE INTERVIEW · Seen Sep 2026
- Binary Tree Nodes at Distance KONSITE INTERVIEW · Seen Aug 2026
- BST to Sorted Circular Doubly Linked ListONSITE INTERVIEW · Seen Aug 2026
Similar questions at other companies
- Random Bingo Card GeneratorUber · ONSITE INTERVIEWUber interview questions
- Rectangle Point Coverage CountsOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Spiral MatrixAthenahealth · ONSITE INTERVIEW
- Explore an Unknown Grid and Find a PathLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Spiral MatrixNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
- Tic-Tac-Toe Move WinnersByteDance · ONSITE INTERVIEWByteDance interview questions
- Trace Every Water Drop to Its Resting DestinationGoogle · ONSITE INTERVIEWGoogle interview questions
- Stochastic Gradient Descent with MomentumMeta · ONSITE INTERVIEWMeta interview questions
- Cyclic Fuel Command SimulationWaymo · ONSITE INTERVIEWWaymo interview questions
- Generate a Spiral MatrixMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
Apple interview experiences
- Apple SWE Phone Screen: Subarray Products: 1 Experience
- Apple New Grad SWE Java, Coding and Manager Rounds: 1 Experience
- Apple AI Coding and Related Interviews: 17 Experiences
- Apple India SWE New Grad Interviews: 5 Experiences
- Apple New Grad SWE Phone Screens: 12 Experiences (Part 3 of 3)
- Apple New Grad SWE and Embedded: 16 Experiences (Part 2 of 3)