File System Shell Commands
Updated
Problem statement
Build a file system held entirely in memory and run it with terminal-style commands. It holds directories and files. Files have names but no contents. At first it holds just the root directory /, which is also the current directory.
You are given commands. Run them in order and return an array with one string per command: what that command prints. A command that succeeds without printing anything gives "". A command that fails gives "ERROR" and changes nothing, not even the current directory.
Paths work like this:
- A path beginning with
/is read from the root. Any other path is read from the current directory. - A path is read one part at a time, splitting it at
/. Empty parts, from repeated slashes or a slash at the end, are skipped. .means the directory reached so far and..means its parent. At the root,..leads back to the root itself.- Every part, whether a name,
.or.., is applied to the item reached so far, and that item must be an existing directory. So a path fails if any part comes after a file or after a missing name, even when a later..would lead back out. - The last part may be a name that does not exist yet. Whether that is allowed depends on the command.
- A path that ends in
/must name a directory: an existing one, or a new one that the command creates.
The commands:
mkdir pathmakes a new, empty directory. It fails if something already exists atpath, or if the directory that should hold it does not exist.touch pathcreates an empty file. If a file or directory already exists atpath, nothing changes and the command still succeeds. It fails if the directory that should hold the new file does not exist, or ifpathends in/and names nothing yet.cd pathmakes the directory atpaththe current directory. It fails ifpathnames a file or nothing.lsprints the names of the items in the current directory, andls pathprints them for the directory atpath. Files and directories are listed together, sorted by character code (so.comes before digits, digits before_, and_before letters), and joined with single spaces. An empty directory prints"". Ifpathnames a file,lsprintspathexactly as it was typed. It fails ifpathnames nothing.cp source targetcopies a file.cp -r source targetcopies a file, or a directory with everything inside it.mv source targetmoves a file, or a directory with everything inside it.
cp and mv first find the destination:
- If
targetis an existing directory, the source goes inside it and keeps its own name. The destination is that directory's item with the source's name, which may or may not exist already. - Otherwise, if
targetis an existing file, that file is the destination. - Otherwise
targetis a new name and the destination is that path. The directory that should hold it must exist, and iftargetends in/, the source must be a directory.
The command fails if the source names nothing or is the root, if cp without -r gets a directory, if the destination is the source itself, if the source is a directory and the destination is inside it, or if the destination exists and is a file while the source is a directory, or the other way around.
When cp succeeds:
- If the destination does not exist, a copy of the source is created there, named after the destination.
- A file copied onto an existing file succeeds, and nothing visible changes, since files are empty.
- A directory copied onto an existing directory is merged into it: every item inside the source is copied into the existing directory by these same rules. Directories with the same name merge, files with the same name stay, and other items are added. If anywhere in the merge a file meets a directory with the same name, the whole command fails.
- The copy shows the source as it was when the command started.
When mv succeeds, the source is taken out of its old place and put at the destination under the destination's name. If the destination exists, the source replaces it. A file may replace a file, and a directory may replace only an empty directory: moving a directory onto a directory that has items in it fails.
The current directory is the directory itself, not its path. If it, or a directory above it, is moved, you are still in it, under its new path.
Function
runCommands(commands: String[]) → String[]Examples
Example 1
commands = ["mkdir /docs","mkdir docs/2026","touch docs/2026/brief.txt","touch docs/notes","cd docs","ls","cd 2026/..","ls ./2026","ls /docs/notes"]return = ["","","","","","2026 notes","","brief.txt","/docs/notes"]You start in /, so docs/2026 means /docs/2026. ls in /docs prints 2026 notes: digits come before letters. cd 2026/.. goes into 2026 and back up, so you stay in /docs. ls on a file prints the path exactly as it was typed.
Example 2
commands = ["mkdir /a/b","mkdir /a","mkdir /a","touch /a/f","cd /a/f","ls /a/f/","ls /a/missing/..","cd /..","ls"]return = ["ERROR","","ERROR","","ERROR","ERROR","ERROR","","a"]mkdir /a/b fails because /a does not exist yet, and the second mkdir /a fails because /a already exists. cd /a/f fails because f is a file, and /a/f/ fails because a path ending in / must name a directory. /a/missing/.. fails because missing must exist before .. can leave it. /.. is the root itself.
Example 3
commands = ["mkdir /src","touch /src/main.py","mkdir /src/lib","touch /src/lib/util.py","cd /src/lib","cp /src /backup","cp -r /src /backup","mv /backup /src","ls /src/backup","ls","mv /src /code","ls","ls /code/lib/.."]return = ["","","","","","ERROR","","","lib main.py","util.py","","util.py","backup lib main.py"]cp without -r cannot copy a directory. cp -r creates /backup with everything inside. /src is an existing directory, so mv /backup /src moves it to /src/backup. You are in /src/lib when /src is renamed to /code, so you stay in the same directory, now /code/lib.
Constraints
1 <= commands.length <= 1000- Each
commands[i]is one ofmkdir path,touch path,cd path,ls,ls path,cp source target,cp -r source targetormv source target, with exactly one space between its parts. - Every path is
1to60characters long and uses only lowercase English letters, digits,.,_and/. - Every name in a path, other than
.and.., is1to10characters long and starts with a letter or a digit. - The last part of a
cpormvsource, ignoring any/at the end, is never.or... mvnever replaces the current directory.- At any time there are at most
2000files and directories, not counting the root. - At any time, no file or directory is more than
100levels below the root (/ais one level below it).
More Harvey Onsite Interview questions
- Highlight and Rank Overlapping CitationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Spreadsheet Formula EvaluatorPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Retrieve Vectors by Cosine SimilarityONSITE INTERVIEW · Seen Nov 2026
- In-Memory Vault File SystemONSITE INTERVIEW · Seen Nov 2026
- Evaluate an Expression MapONSITE INTERVIEW · Seen Sep 2026
- Find Duplicate Files in a FilesystemPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Highlight Matching PhrasesPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Dictionary Records to an Editable XML TreeSnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions
- Account Scheduler - Availability and AcquireStripe · ONSITE INTERVIEWStripe interview questions
- In-Memory Directory and File OperationsSquarepoint Capital · ONSITE INTERVIEWSquarepoint Capital interview questions
- In-Memory Spreadsheet ValuesRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- In-Memory File System With Directory SizesCapital One · PHONE SCREEN · ONSITE INTERVIEWCapital One interview questions
- Feature Flag Evaluation EngineCresta · ONSITE INTERVIEWCresta interview questions
- Return Every Matching DecryptionDuolingo · PHONE SCREENDuolingo interview questions
- Parcel Event TrackingAirbnb · OAAirbnb interview questions
- Hierarchical In-Memory File SystemClay · PHONE SCREEN