Implement a file directory tree with mkdir, cd, pwd, ls, readFile and delete
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design and implement a file directory tree management system. The system starts with a single root directory `/` and supports creating directories, adding files with content, navigating the tree, listing directory contents, reading files, and deleting entries.
All operations work relative to the current working directory unless an absolute path (starting with `/`) is provided. Navigation supports `..` to move to the parent directory and `.` to refer to the current directory.
Directory and file names consist of alphanumeric characters, dots, and underscores. Names within the same directory are unique.
Implement a class `FileTree` with the following methods:
- `mkdir(name: str) -> bool`: Create a new directory with the given name in the current working directory. Returns True if created successfully, False if the name already exists.
- `addFile(name: str, content: str) -> bool`: Add a new file with the given name and content in the current working directory. Returns True if created successfully, False if the name already exists.
- `cd(path: str) -> bool`: Change the current working directory to the specified path. Supports `..` for parent, `.` for current, and a `/` prefix for absolute paths. Returns True if navigation succeeded, False otherwise.
- `ls() -> str`: List the contents of the current directory as a comma-separated sorted string of names. Returns an empty string if the directory is empty.
- `pwd() -> str`: Return the full absolute path of the current working directory.
- `readFile(name: str) -> str`: Read and return the content of the named file in the current directory. Returns an empty string if the file does not exist or the name refers to a directory.
- `delete(name: str) -> bool`: Delete the named file or directory from the current directory. Returns True if deleted, False if not found or if the target is the current working directory or an ancestor of it.
The first example covers directory navigation:
```text
Input:
["FileTree", "mkdir", "cd", "mkdir", "cd", "pwd", "cd", "pwd", "cd", "delete"]
[[], ["a"], ["a"], ["b"], ["b"], [], [".."], [], ["/"], ["nonexistent"]]
Output: [null, true, true, true, true, "/a/b", true, "/a", true, false]
Explanation:
ft = FileTree() # root directory "/"
ft.mkdir("a") # True (created /a)
ft.cd("a") # True (cwd is now /a)
ft.mkdir("b") # True (created /a/b)
ft.cd("b") # True (cwd is now /a/b)
ft.pwd() # "/a/b"
ft.cd("..") # True (cwd is now /a)
ft.pwd() # "/a"
ft.cd("/") # True (cwd is now /)
ft.delete("nonexistent") # False (name not found)
```
The second example covers files, listing and deletion:
```text
Input:
["FileTree", "addFile", "mkdir", "addFile", "ls", "readFile", "readFile", "cd", "cd", "pwd", "readFile", "cd", "delete", "ls"]
[[], ["notes.txt", "hello"], ["docs"], ["docs", "x"], [], ["notes.txt"], ["docs"], ["notes.txt"], ["/docs"], [], ["notes.txt"], [".."], ["notes.txt"], []]
Output: [null, true, true, false, "docs,notes.txt", "hello", "", false, true, "/docs", "", true, true, "docs"]
Explanation:
ft = FileTree() # root directory "/"
ft.addFile("notes.txt", "hello") # True (created /notes.txt)
ft.mkdir("docs") # True (created /docs)
ft.addFile("docs", "x") # False (the name is taken by a directory)
ft.ls() # "docs,notes.txt"
ft.readFile("notes.txt") # "hello"
ft.readFile("docs") # "" (docs is a directory)
ft.cd("notes.txt") # False (a file is not a directory; cwd stays /)
ft.cd("/docs") # True (absolute path)
ft.pwd() # "/docs"
ft.readFile("notes.txt") # "" (no such file in /docs)
ft.cd("..") # True (cwd is now /)
ft.delete("notes.txt") # True
ft.ls() # "docs"
```
```hint One resolver
Only `cd` takes a path. Write the walk from a starting node over the path segments as one helper, and decide what each kind of segment (empty, `.`, `..`, a name) does to the current position.
```
```hint Getting back up
Think about how `..` and `pwd` can move from a node toward the root without searching the tree from the top.
```
```hint Walking through a file
Trace `cd("notes.txt/..")` through your resolver. A file has no parent directory to step back into as far as the path is concerned.
```
### Constraints & Assumptions
- `1 <= name.length <= 100`
- `1 <= content.length <= 1000`
- Names contain only alphanumeric characters, dots (`.`), and underscores (`_`).
- At most 1000 operations in total.
- Paths use `/` as the separator.
### Clarifying Questions to Ask
- What should `cd("..")` do at the root: stay at `/` and return True, as a Unix shell does, or fail?
- The name rules allow dots, so may a caller create an entry named `.` or `..`, which would clash with the navigation segments?
- Should `ls` sort by character code (uppercase before lowercase) or ignore case?
- Does `delete` on a non-empty directory remove the whole subtree, or should it fail?
- Should `cd` accept trailing or repeated slashes such as `a//b/`?
### What a Strong Answer Covers
- A node model that separates directories (a map of children) from files (content) and lets a node reach its parent.
- A single path resolver that handles absolute and relative paths, `.`, `..`, empty segments, missing names, and paths that pass through a file.
- How `pwd` is computed and how `ls` produces a defined sort order.
- The exact return value of every method on its failure cases, including why the delete guard holds.
- Time and space cost per operation.
### Follow-up Questions
- How would `mkdir`, `addFile` and `delete` change if they accepted full paths, and what check does `delete` need then?
- How would you add `mv(src, dst)`, and what prevents moving a directory into its own subtree?
- How would you support creating all missing intermediate directories in one call, like `mkdir -p`?
- If directories held hundreds of thousands of entries and `ls` was called constantly, how would you avoid sorting on every call?
Overview: Implement an in-memory file directory tree that starts at a root and supports mkdir, addFile, cd, pwd, ls, readFile and delete relative to a current working directory. It tests tree modeling, path resolution with absolute paths and the dot and double-dot segments, and exact return values on failure cases.