In-Memory File System With Directory Sizes
Updated
Problem statement
Build an in-memory file system that starts with just the root directory /. A path is / or one or more names, each written after a slash, such as /a/b/c. Run the entries of commands in order; each is one of:
"MKDIR path": make directorypathtogether with any missing directories above it. Ifpath, or a name on the way to it, is already a file, return"ERROR"and change nothing. Otherwise return"OK", also when the directory already exists."ADD path size": store a file ofsizeatpath. Its parent directory must already exist, and nothing, neither a file nor a directory, may be stored atpathyet. Otherwise return"ERROR"and change nothing. If the file is stored, return"OK"."SIZE path": for a file, return its size; for a directory, return the total size of all files anywhere inside it (0if it holds none). The answer is a decimal string. Return"ERROR"if nothing is stored atpath."LS path": for a directory, return the names directly inside it, files and directories alike, sorted by character code and joined with commas (so digits come before letters, and a name comes before a longer name that starts with it). An empty directory gives the empty string. Return"ERROR"ifpathis a file or nothing is stored there.
Return one string per command, in the order of commands.
Function
runFileCommands(commands: String[]) → String[]Examples
Example 1
commands = ["MKDIR /docs/work","ADD /docs/work/plan 40","ADD /docs/notes 15","ADD /docs/work/a1 5","SIZE /docs","SIZE /docs/work/plan","LS /docs","LS /docs/work","SIZE /"]return = ["OK","OK","OK","OK","60","40","notes,work","a1,plan","60"]MKDIR /docs/work also creates /docs, the directory above it. The three files hold 40, 15 and 5, so SIZE /docs adds up the file right inside it and the two files one level further down: 60. SIZE /docs/work/plan is that file's own 40. A listing shows only the entries directly inside a directory, in alphabetical order even though plan was added before a1. The root holds the same 60.
Example 2
commands = ["ADD /top 7","ADD /a/b 3","MKDIR /a","ADD /a/b 3","ADD /a/b 4","MKDIR /top/sub","LS /top","SIZE /zzz","LS /a/x","LS /a","SIZE /a"]return = ["OK","ERROR","OK","OK","ERROR","ERROR","ERROR","ERROR","ERROR","b","3"]/a does not exist yet, so the first ADD /a/b 3 fails and creates nothing: once MKDIR /a has run, the same command works, and repeating it with another size fails because the path is taken. /top is a file, so MKDIR /top/sub fails and LS /top fails too, since only directories can be listed. SIZE /zzz and LS /a/x name nothing stored. At the end /a holds only b, of size 3.
Example 3
commands = ["LS /","SIZE /","MKDIR /x/y","ADD /x/f 2","MKDIR /x/f","ADD /x/y 9","MKDIR /x","MKDIR /x/y/z","SIZE /x/y","LS /x","LS /x/y"]return = ["","0","OK","OK","ERROR","ERROR","OK","OK","0","f,y","z"]A fresh root is empty: LS / returns the empty string and SIZE / returns 0. /x/f is a file, so MKDIR /x/f fails, and /x/y is a directory, so ADD /x/y 9 fails; neither changes anything. MKDIR /x on a directory that already exists is fine. The directory /x/y still holds no file, so its size is 0; /x lists f,y and /x/y lists z.
Constraints
1 <= commands.length <= 2000- Each command has one of the four forms above, with single spaces, and is 4 to 120 characters long.
- Every path is
/or one to 12 names after slashes, each name 1 to 8 lowercase letters or digits. Everysizeis a whole number from1to10^4.
More Capital One interview questions
- Count Records Present in A but Not BPHONE SCREEN · Seen Oct 2026
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen May 2026
- Maximum Subarray SumPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2025
- Ten-Pin Bowling Frame ScoresPHONE SCREEN · Seen Nov 2023
- Min StackPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2023
- Invert a Binary TreePHONE SCREEN · Seen Dec 2020
- Count How Often a Word Appears in a ParagraphPHONE SCREEN
- Pick the K Nearest Grid Points to (0, 0)PHONE SCREEN · ONSITE INTERVIEW
- The Nth Number in the Fibonacci SequencePHONE SCREEN · ONSITE INTERVIEW
- Digit-Coded Card ValidationONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Workspace File System With Folders and TablesClay · PHONE SCREEN
- Office Employee HierarchyBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Random Dasher Set OperationsDoorDash · ONSITE INTERVIEWDoorDash interview questions
- LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Tree Selection and VisibilityJane Street · PHONE SCREENJane Street interview questions
- Rectangle Point Coverage CountsOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- In-Memory Vault File SystemHarvey · ONSITE INTERVIEWHarvey interview questions
- File Storage ServiceCresta · PHONE SCREENCresta interview questions
- Hash Map with Separate ChainingApplied Intuition · ONSITE INTERVIEWApplied Intuition interview questions
Capital One interview experiences
- Capital One Experienced Engineer Power Day and OA: 6 Experiences
- Capital One New Grad OA Questions: 14 Experiences (Part 2 of 2)
- Capital One TDP Agentic Round (AI Coding): 7 Experiences
- Capital One SWE Power Day, Level Not Reported: 6 Experiences
- Capital One TIP Intern OA and Power Day: 13 Experiences
- Capital One TDP Agentic Power Day: 25 Experiences (Part 1 of 2)