Medium · Streams & I/O

Clean directories recursively

Given the path of a root directory, delete every directory in its tree, the root included, that is empty or becomes empty once its empty subdirectories are deleted; files are never deleted, and the answer is the tree left on disk.

Examples

Example 1

{
  "root": {
    "name": "root",
    "files": [],
    "children": [
      {"name": "a", "files": [], "children": [{"name": "b", "files": [...], "children": [...]}]},
      {"name": "keep", "files": ["data.txt"], "children": []}
    ]
  }
}

Output: ["root/keep/","root/keep/data.txt"]

Example 2

{
  "root": {
    "name": "root",
    "files": ["x.txt"],
    "children": [
      {"name": "empty", "files": [], "children": []}
    ]
  }
}

Output: ["root/x.txt"]

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More Streams & I/O problems