Solve Flower Placement and Directory Deletion
GoogleSoftware Engineer · OnsiteLast updated
Implement algorithms for two interview tasks: minimal directory deletion in a hierarchical filesystem using tree traversal, and valid flower placement on a grid using constraint satisfaction.
Problem Statement
In this interview session, you are tasked with solving two independent algorithmic problems commonly encountered in system cleanup and spatial layout design.
Task 1: Directory Deletion
You are given a list of all existing directory paths in a filesystem, all_paths, and a subset of paths to remove, to_delete. Deleting a directory automatically deletes all of its subdirectories. You must find the minimum number of directory deletion commands to remove every path in to_delete without deleting any path not in to_delete.
Each path is represented as a string starting with / and separated by / (e.g., "/a/b/c").
def delete_directories(all_paths: list[str], to_delete: list[str]) -> list[str]:
Constraints (Task 1)
1 <= len(all_paths) <= 10^41 <= len(to_delete) <= len(all_paths)- All paths in
to_deleteare present inall_paths. - Path names contain only lowercase English letters and
/.
Task 2: Flower Placement Grid
You are designing a garden layout represented by an m x n grid. Each cell is either 0 (empty plantable plot) or -1 (obstacle/rock). You are given k flower species labeled 1 through k. You must assign a flower species to every empty plot such that no two horizontally or vertically adjacent plots have the same species.
Return True if a valid assignment is possible, or False otherwise.
def can_place_flowers(grid: list[list[int]], k: int) -> bool:
Constraints (Task 2)
1 <= m, n <= 101 <= k <= 4grid[i][j]is either0or-1.
Examples
Example 1: Directory Deletion
all_paths = ["/a/b/c", "/a/b/d", "/a/e"]
to_delete = ["/a/b/c", "/a/b/d"]
# Output: ["/a/b"]
Explanation: Deleting /a/b removes both /a/b/c and /a/b/d in a single command without removing /a/e.
Example 2: Flower Placement
grid = [
[0, 0],
[0, -1]
]
k = 2
# Output: True
Explanation: We can place species 1 at (0,0), species 2 at (0,1), and species 2 at (1,0). No adjacent cells share the same species.
Edge Cases
- Task 1: When all existing paths are marked for deletion, the root
/(or top-level directories) should be deleted. - Task 1: A directory where only some children are deleted cannot itself be deleted; deletions must occur deeper in the tree.
- Task 2: Grid with no empty cells (
0count is 0) should returnTrue. - Task 2:
k = 1with two adjacent empty cells should returnFalse.
Hints
Hint 1
For directory deletion, build a Trie or tree structure where each node indicates whether all its descendants are marked for deletion, allowing you to prune the highest valid ancestor.
Hint 2
For flower placement, this is equivalent to graph vertex coloring; utilize backtracking with forward checking (MRV heuristic or basic constraint propagation).
Hint 3
Notice the small grid dimensions in Task 2: pruning invalid color choices early via depth-first search ensures fast termination.
Follow-up questions
- How would you refactor Task 1 iteratively to avoid stack overflow when the directory depth is extremely large (e.g., millions of nested directories)?
- For Task 2, how does the complexity change if the grid is bipartite (e.g., no obstacles or odd cycles), and what is the minimum k required for any grid?
- In Task 1, if deleting a folder has an associated non-uniform cost (e.g., API latency per deletion), how would you minimize total deletion cost?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.