Solve Flower Placement and Directory Deletion
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates combinatorial constraint-satisfaction and permutation/matching skills for the flower-placement grid alongside filesystem traversal and recursive versus iterative deletion strategies for the directory-deletion task.
Part 1: Place Flowers on a Grid
Constraints
- 1 <= N <= 8
- grid.length == N
- Each row has length N
- Each character is either 'H' or '0'
Examples
Input: (['0'],)
Expected Output: ['F']
Explanation: Single-cell grid with one empty cell. The only valid placement is to put the flower there.
Input: (['H'],)
Expected Output: []
Explanation: The only cell is a house, so a flower cannot be placed, but the row and column still need one flower. No solution exists.
Input: (['00', '00'],)
Expected Output: ['F0', '0F']
Explanation: There are multiple perfect row/column placements, but the lexicographically smallest flower-column sequence is [0, 1].
Input: (['H000', 'H000', '000H', '0000'],)
Expected Output: ['HF00', 'H0F0', 'F00H', '000F']
Explanation: The flowers are placed in columns [1, 2, 0, 3], which satisfies the row, column, and house-adjacency rules.
Input: (['H0', '00'],)
Expected Output: []
Explanation: If the flower in row 0 is placed at column 1, the house at (0, 0) becomes adjacent to two flowers: (0, 1) and (1, 0). So no valid grid exists.
Hints
- Because each row and each column must contain exactly one flower, a solution corresponds to choosing a permutation of columns.
- For each candidate permutation, check two things: no flower lands on a house, and every house sees exactly one adjacent flower.
Part 2: Delete a Directory and All Descendants
Constraints
- 0 <= len(directories) == len(children_lists) <= 200000
- The total number of child-path entries across all lists is at most 200000
- Directory paths in directories are unique
- The filesystem structure is acyclic
- Each child path has at most one parent
Examples
Input: (['/root', '/root/a', '/root/b'], [['/root/a', '/root/file.txt', '/root/b'], ['/root/a/x'], []], '/root')
Expected Output: ['/root/a/x', '/root/a', '/root/file.txt', '/root/b', '/root']
Explanation: Delete descendants first, preserving child order: subtree /root/a, then /root/file.txt, then /root/b, and finally /root.
Input: (['dir'], [[]], 'dir')
Expected Output: ['dir']
Explanation: An empty directory can be deleted immediately.
Input: (['dir'], [['dir/file.txt']], 'dir/file.txt')
Expected Output: ['dir/file.txt']
Explanation: The target is already a file, so only one delete call is needed.
Input: (['dir'], [['dir/file.txt']], 'missing')
Expected Output: []
Explanation: The path does not appear as a directory or as any listed child, so it does not exist in this filesystem.
Input: (['a', 'a/b', 'a/b/c'], [['a/b'], ['a/b/c', 'a/b/d'], []], 'a/b')
Expected Output: ['a/b/c', 'a/b/d', 'a/b']
Explanation: Delete the empty directory a/b/c, then the file a/b/d, and then delete a/b itself.
Hints
- A directory can only be deleted after its contents, so the required order is a postorder DFS traversal.
- To avoid recursion-depth issues on deep directory trees, use an explicit stack with a visited/expanded flag.