The core move in every file-system problem is turning a path like "/docs/2024/notes.txt" into a walk through nested dicts: split on "/", and step one dict deeper per directory name.
Implement FileTree() with:
add_file(path, size): store a file ofsizebytes atpath, creating any missing parent directories. If the file already exists, its size is replaced.list_dir(path) -> list[str]: the names of the direct children (files and directories) of the directory atpath, sorted."/"is the root.dir_size(path) -> int: the total size of every file anywhere under the directory atpath(0 for an empty directory).
t = FileTree()
t.add_file("/docs/a.txt", 10)
t.add_file("/docs/old/b.txt", 5)
t.add_file("/c.txt", 1)
t.list_dir("/") # ["c.txt", "docs"]
t.list_dir("/docs") # ["a.txt", "old"]
t.dir_size("/docs") # 15
t.dir_size("/") # 16
Paths are absolute, have no trailing slash (except the root "/"), and names are non-empty. You may assume list_dir and dir_size are only called on directories that exist, and that nobody adds a file where a directory is (or the other way round).
Show hint
represent a directory as a dict from child name to either another dict (a subdirectory) or an int (a file's size), and walk it with path.split("/"), skipping empty parts.