Matrix Flower-House Planting Plans
Updated
Problem statement
You are given a rectangular grid containing H (house), F (an existing flower), and O (an empty plot). You may change selected O cells to F; houses and existing flowers cannot be changed.
A final grid is valid when:
- Every row and every column contains at most one flower.
- Every house has exactly one orthogonally adjacent flower.
- Every flower is orthogonally adjacent to exactly one house, so no flower is shared by two houses and no irrelevant flower is planted.
Return every valid final grid. Encode one grid by joining its rows with /, and return the encodings in lexicographic order. Return an empty array when no valid planting exists.
The base interview question asks only whether a valid planting exists. The returned list implements the follow-up that asks for every possible planting; the base answer is true exactly when the returned list is nonempty.
Function
flowerHousePlans(grid: String[]) → String[]Examples
Example 1
grid = ["HOO","OOH"]return = ["HOF/FOH"]The top-left house uses the flower at row 1, column 0, and the bottom-right house uses the flower at row 0, column 2. The flowers occupy different rows and columns.
Example 2
grid = ["HOO","OOO","OOH"]return = ["HFO/OOF/OOH","HOO/FOO/OFH"]There are two valid ways to give each house one exclusive neighboring flower while keeping flower rows and columns unique.
Example 3
grid = ["HFH"]return = []The existing flower touches both houses, so it cannot belong exclusively to either one.
Constraints
1 <= grid.length <= 6.1 <= grid[r].length <= 6, and all rows have equal length.- Every cell is
H,F, orO. - There are at most
10houses. - The number of valid returned grids is at most
10000.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Cats, Rabbits, and Snails Board GameAsana · ONSITE INTERVIEW
- Boggle Word FinderBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Enumerate Character Paths to a MarkerTesla · ONSITE INTERVIEWTesla interview questions
- Eight-Direction Word SearchMotive · ONSITE INTERVIEWMotive interview questions
- Trace a Word Through a Letter GridExpedia · ONSITE INTERVIEWExpedia interview questions
- Word Search with Eight-Direction AdjacencyMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Find a Word in a Letter GridAdobe · PHONE SCREENAdobe interview questions
- Optimal Account BalancingAmazon · ONSITE INTERVIEW
- Sudoku Puzzle SolverConfluent · PHONE SCREEN · ONSITE INTERVIEWConfluent interview questions
- Optimal Account BalancingPinterest · PHONE SCREEN · ONSITE INTERVIEWPinterest interview questions