Design an In-Memory File Manager with Atomic Move and Wildcard Search
Company: CRED
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design an in-memory file manager that keeps a rooted tree of directories and files. The root directory `/` always exists. Directories and files are created one level at a time, so a missing parent is never created automatically. Files have no contents.
Deleting a node removes its whole subtree. A move (or rename) relocates a node and its subtree to an exact destination path. A move is atomic: if it is rejected because the source is `/` or missing, the destination already exists, the destination's parent is missing or is a file, or a directory would move into its own subtree, the tree stays unchanged.
The manager also supports wildcard search on basenames below a given directory. In a pattern, `?` matches exactly one character and `*` matches zero or more characters. Matching is case-sensitive and must cover the whole name. An empty pattern matches nothing. Results are absolute paths sorted in ascending ASCII order, and the search root itself is never included.
Implement the following methods:
- `mkdir(path: str) -> bool`: create a directory at `path`. Return `False` if the path exists or its immediate parent is missing or is a file.
- `createFile(path: str) -> bool`: create an empty file at `path`. Return `False` if the path exists or its immediate parent is missing or is a file.
- `delete(path: str) -> bool`: recursively remove the file or directory at `path`. Return `False` for the root or a missing path.
- `move(source: str, destination: str) -> bool`: move or rename the node at `source`, with its subtree, to the exact `destination` path. Return `False` and change nothing if the move is invalid.
- `search(rootPath: str, pattern: str) -> List[str]`: return the sorted absolute paths of all nodes strictly below `rootPath` whose basename matches the wildcard pattern. Return `[]` if `rootPath` is missing or is a file.
```hint Storing paths
Think about what a move would cost if every node stored its full path, and what each node could store instead so that a move changes a single link.
```
```hint Validate before mutating
List every reason a move can fail and check all of them before touching the tree. When testing whether the destination lies inside the source, compare paths segment by segment.
```
```hint Matching names
Plain backtracking on `*` can take exponential time on patterns such as `a*a*a*b`. Consider a table indexed by positions in the name and in the pattern.
```
### Examples
Example 1:
```text
mkdir("/docs") -> True
mkdir("/docs/2024") -> True
createFile("/docs/a.txt") -> True
createFile("/docs/2024/b.txt") -> True
createFile("/docs/2024/notes.md") -> True
mkdir("/docs") -> False
mkdir("/tmp/x") -> False
createFile("/docs/a.txt/c") -> False
search("/", "*.txt") -> ["/docs/2024/b.txt", "/docs/a.txt"]
search("/docs", "?.txt") -> ["/docs/2024/b.txt", "/docs/a.txt"]
search("/docs", "*.TXT") -> []
search("/docs/a.txt", "*") -> []
delete("/docs/2024") -> True
search("/", "*") -> ["/docs", "/docs/a.txt"]
```
`/docs` already exists, `/tmp` is missing and `/docs/a.txt` is a file, so those three creates fail. "/docs/2024/b.txt" sorts before "/docs/a.txt" because "2" comes before "a" in ASCII. Matching is case-sensitive, so "*.TXT" finds nothing, and a file cannot be a search root. Deleting "/docs/2024" also removes the two files inside it.
Example 2:
```text
mkdir("/a") -> True
mkdir("/a/b") -> True
createFile("/a/b/f.log") -> True
mkdir("/c") -> True
move("/a", "/a/b/a2") -> False
move("/a/b", "/c") -> False
move("/a/b", "/c/b2") -> True
search("/", "*") -> ["/a", "/c", "/c/b2", "/c/b2/f.log"]
move("/", "/z") -> False
move("/c/b2/f.log", "/f.log") -> True
search("/", "f*") -> ["/f.log"]
```
The first move would put `/a` inside its own subtree, and the second targets `/c`, which already exists, so both leave the tree unchanged. The third renames `b` to `b2` while moving it under `/c`, and `f.log` moves with it. The root can never be a move source.
### Constraints & Assumptions
- Names contain 1 to 24 characters from `[A-Za-z0-9_.]` and are never `.` or `..`.
- All paths are normalized absolute paths with no trailing slash (except `/`).
- Paths have at most 40 segments.
- The total number of nodes is at most 2,000.
- The total number of operations is at most 2,000.
- `0 <= len(pattern) <= 40`, and patterns contain only name characters, `?` and `*`.
- Matching is case-sensitive.
### What a Strong Answer Covers
- A node structure for directories and files, and how a path is resolved from the root and at what cost.
- Create and delete rules: one level at a time, rejection when the parent is missing or is a file, and removal of the whole subtree on delete.
- Every rejection case for `move`, all checked before any mutation, including a correct test for moving a directory into its own subtree.
- A wildcard matcher for `?` and `*` that covers the whole name without exponential backtracking, and the empty-pattern rule.
- A search that excludes its root, returns absolute paths in ASCII order, and returns `[]` for a missing root or a file root.
### Follow-up Questions
- How would you add file contents with `read` and `write` methods, and does the move logic change?
- Searches happen far more often than changes to the tree. What would you index or cache?
- How would you add a `mkdir -p` mode that creates missing parents, and keep it atomic when it fails partway?
- How would you make the manager safe for concurrent callers, especially `move`?
Overview: Design an in-memory file manager that creates directories and files, deletes whole subtrees, moves nodes atomically, and searches basenames with ? and * wildcards. It tests tree modeling, validating every failure case before changing state, and wildcard matching that avoids exponential backtracking.