In-Memory File System with Shell Commands, Thread-Safe cp/mv, and Multi-Device Sync
Company: Harvey
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Onsite
Build an in-memory file system that is driven by terminal-like commands. That one sentence was the whole prompt, so choosing which commands to support was part of the exercise; the commands below are the ones the round settled on. The round combined live coding, low-level design and system design: write working code for the first two parts, then discuss concurrency and scale.
A possible starting interface (names may differ, but every command takes shell-style path strings):
```python
class FileSystem:
def cd(self, path: str = "/") -> None: ...
def mkdir(self, path: str) -> None: ...
def ls(self, path: str = ".") -> list[str]: ...
def touch(self, path: str) -> None: ...
def cp(self, src: str, dst: str) -> None: ... # Part 2
def mv(self, src: str, dst: str) -> None: ... # Part 2
```
### Clarifying Questions
- Which commands are required, and how should errors be reported: exceptions, shell-style error messages, or silent no-ops?
- Do files have contents, or only names? Is there a command to write or print a file?
- Must paths support both absolute and relative forms, with `.` and `..`? What does `..` mean at the root?
- Should `ls` return names sorted, and what should `ls` on a file return?
- Should `mkdir` create missing parent directories, and what should `touch` do to an existing file?
### Part 1: cd, mkdir, ls, touch
Implement a tree of directories and files with a current working directory: `mkdir` creates a directory, `touch` creates an empty file, `cd` changes the current directory, and `ls` lists a directory's entries.
```hint Shape of a directory
Decide what a directory node needs to hold so that walking a path costs one lookup per component, and how the current directory is remembered.
```
```hint One resolver for every command
Every command takes a path. Consider one routine that turns any absolute or relative path, including `.` and `..`, into a node, and a sibling that returns the parent directory plus the final name.
```
#### What This Part Should Cover
- The node model for directories and files, and how the current directory is held
- Path resolution for absolute and relative paths, `.` and `..`
- Error behavior for missing paths, files used as directories, and names that already exist
- The cost of each command in terms of path depth and directory size
### Part 2: cp and mv
Add `cp`, which copies a file or a whole directory, and `mv`, which moves or renames one.
```hint Copy versus move
One of the two commands must duplicate a whole subtree; the other only re-links one node. Think about what must be checked before re-linking a directory.
```
#### Clarifying Questions for this Part
- If the destination is an existing directory, should the source go inside it under its own name? If it is an existing file, may it be overwritten?
- Should copying a directory be recursive by default?
#### What This Part Should Cover
- Deep copy versus re-linking, and what each costs
- Destination rules: an existing directory, an existing file, a new name
- Invalid operations, such as moving the root or moving a directory into its own subtree
- What happens to the current directory when it, or one of its ancestors, is moved
### Part 3: Threads and locking in cp and mv
Several threads now run commands against the same file system at the same time. Explain, and sketch in code, how you keep `cp` and `mv` correct.
```hint Two directories at once
A move changes two parent directories. Picture two threads, each moving an entry into the other's directory, and ask what rule about the order of taking locks would stop them from blocking each other forever.
```
#### What This Part Should Cover
- The shared state each command reads and writes, and where each client's current directory lives
- Lock granularity: one global or reader-writer lock versus per-directory locks
- Deadlock avoidance, and re-validation after the locks are acquired
- A consistent view of the source while `cp` copies a large subtree
### Part 4: Scaling to a cloud drive with multi-device sync
How would you turn this into a Google Drive-like service, and how would files stay in sync across a user's devices?
```hint What a device really holds
Think about which copy is the source of truth, what a device sends after editing offline, and what happens when two devices change the same file before either has synced.
```
#### What This Part Should Cover
- Separating file metadata (the tree) from file contents, and how each is stored
- How a move or rename works once the tree lives in a database
- How devices learn about changes and upload only what changed
- Conflicts and offline edits
### What a Strong Answer Covers
- Asking which commands and behaviors are wanted before writing code
- Working code extended step by step, with a single path resolver and clear errors
- Correct edge cases: `..` at the root, files used as directories, existing names, moves into a subtree
- Concurrency reasoning that is free of races and deadlocks, with a stated trade-off between simplicity and parallelism
- A scale-out design that reuses the tree ideas, with a concrete sync protocol and conflict policy
### Follow-up Questions
- How would you add file contents, with commands to write and print a file, and keep very large files efficient?
- How would you add `rm -r` safely when another thread may be working inside the directory being removed?
- How would you persist the in-memory tree so that it survives a restart?
- A device was offline while another device renamed a folder whose files the first device had edited. How does sync reconcile the two?
Overview: Build an in-memory file system driven by terminal-style commands, starting with cd, mkdir, ls and touch and extending to cp and mv. Follow-ups probe thread safety and locking for concurrent copies and moves, then scaling the design into a cloud drive that keeps files in sync across multiple devices.
Read the full Harvey Software Engineer interview experience this question came from