FastPrepFile System Shell Commands
Problems/File System Shell Commands

File System Shell Commands

Harvey logoHarvey● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Harvey onsite interview for a senior software engineer role, in a coding round where AI help was allowed. The task was a file system kept in memory and driven by shell-style commands: first cd, mkdir, ls and touch, then cp and mv. The interviewer also asked about locking when several threads run cp and mv, about growing the system into a cloud drive, and about keeping files in step across devices.

The report names the six commands only. Every behavior rule here is our choice, based on standard Unix behavior: how paths, . and .. work, what each command prints, when a command fails, that cp needs -r for a directory, how cp merges directories, when mv may replace an item, and the limits. You get the commands as a list of strings and return one output per command. The follow-up questions are covered in the editorial but not tested.

Our version is about 70% the same as the reported question.

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 path makes a new, empty directory. It fails if something already exists at path, or if the directory that should hold it does not exist.
  • touch path creates an empty file. If a file or directory already exists at path, nothing changes and the command still succeeds. It fails if the directory that should hold the new file does not exist, or if path ends in / and names nothing yet.
  • cd path makes the directory at path the current directory. It fails if path names a file or nothing.
  • ls prints the names of the items in the current directory, and ls path prints them for the directory at path. 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 "". If path names a file, ls prints path exactly as it was typed. It fails if path names nothing.
  • cp source target copies a file. cp -r source target copies a file, or a directory with everything inside it. mv source target moves a file, or a directory with everything inside it.

cp and mv first find the destination:

  • If target is 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 target is an existing file, that file is the destination.
  • Otherwise target is a new name and the destination is that path. The directory that should hold it must exist, and if target ends 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 of mkdir path, touch path, cd path, ls, ls path, cp source target, cp -r source target or mv source target, with exactly one space between its parts.
  • Every path is 1 to 60 characters long and uses only lowercase English letters, digits, ., _ and /.
  • Every name in a path, other than . and .., is 1 to 10 characters long and starts with a letter or a digit.
  • The last part of a cp or mv source, ignoring any / at the end, is never . or ...
  • mv never replaces the current directory.
  • At any time there are at most 2000 files and directories, not counting the root.
  • At any time, no file or directory is more than 100 levels below the root (/a is 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

Harvey interview experiences

  • Harvey Experienced SWE Phone Screen: 1 Experience
  • Harvey MLE Phone Screen and Virtual Onsite: 1 Experience
All Harvey interview questionsSee Harvey hiring insights
Saved
class Solution {
    public String[] runCommands(String[] commands) {
        // Write your code here.
    }
}
Checking account…