Design In-Memory File System
Updated
Problem statement
Implement an in-memory file system and execute an ordered array of operations. The file system initially contains only the root directory /.
Each operation is a string array in one of these forms:
["ls", path]: ifpathis a file, return a one-element row containing its filename. If it is a directory, return the names of its immediate files and directories in lexicographic order.["mkdir", path]: create every missing directory alongpath. Existing directories are unchanged.["addContentToFile", filePath, content]: create the file withcontentwhen it does not exist; otherwise appendcontentto the existing file.["readContentFromFile", filePath]: return a one-element row containing the file's complete content.["rm", path]: remove the file or directory atpath. Removing a directory also removes everything inside it. A removed path no longer exists, so a later operation can create it again: a new file starts with no content, and a new directory starts empty.
Only ls and readContentFromFile produce output rows. Return those rows in operation order. All paths are absolute. Every operation is valid when it runs: parent directories exist where required, directory operations target directories, file operations target files, and rm targets an existing path other than /.
Function
executeFileSystem(operations: String[][]) → String[][]Examples
Example 1
operations = [["ls","/"],["mkdir","/a/b/c"],["addContentToFile","/a/b/c/d","hello"],["ls","/"],["readContentFromFile","/a/b/c/d"]]return = [[],["a"],["hello"]]The first root listing is empty. Creating /a/b/c makes a visible at the root, and the final read returns the content stored in file d.
Example 2
operations = [["mkdir","/work/logs"],["addContentToFile","/work/logs/run","hello"],["addContentToFile","/work/logs/run"," world"],["mkdir","/work/cache"],["ls","/work"],["ls","/work/logs/run"],["readContentFromFile","/work/logs/run"]]return = [["cache","logs"],["run"],["hello world"]]The directory listing is sorted, listing a file returns only its own name, and the second write appends to the existing content.
Example 3
operations = [["mkdir","/a/b"],["addContentToFile","/a/b/f","old"],["addContentToFile","/a/g","keep"],["rm","/a/b"],["ls","/a"],["mkdir","/a/b"],["addContentToFile","/a/b/f","new"],["readContentFromFile","/a/b/f"],["rm","/a/g"],["ls","/a"]]return = [["g"],["new"],["b"]]Removing /a/b also removes the file f inside it, so /a lists only g. Creating /a/b/f again starts a new, empty file, so the read returns new and not oldnew. After the file g is removed, /a lists only b.
Constraints
1 <= operations.length <= 2000- Paths are absolute, begin with
/, contain no repeated slash, and have no trailing slash except for/. - Each path component contains
1to30lowercase English letters. - Each content string contains at most
1000lowercase English letters or spaces. - The total length of all operation strings is at most
200000. - No path is used as both a file and a directory, even after it is removed.
- Every operation satisfies the validity rules in the statement.
More Snowflake Phone Screen questions
- Merge Two Binary TreesPHONE SCREEN · Seen Nov 2026
- Connect Four from the Last MovePHONE SCREEN · Seen Nov 2026
- Minimum Edits to a Perfect Binary TreePHONE SCREEN · Seen Nov 2026
- Configurable Per-Client Token BucketsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Find Valid SET Card TriplesOA · PHONE SCREEN · Seen Nov 2026
- Minimum Index Distance Between Person and CakeOA · PHONE SCREEN · Seen Nov 2026
- Minimum N-ary Tree Depth DeletionsOA · PHONE SCREEN · Seen Nov 2026
- Sum of Binary Tree Boundary NodesOA · PHONE SCREEN · Seen Nov 2026
- Greedy Person-Cake Matching DistancePHONE SCREEN · Seen Nov 2026
- Tree Height After Child PromotionPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- File Storage ServiceCresta · PHONE SCREENCresta interview questions
- Return Every Matching DecryptionDuolingo · PHONE SCREENDuolingo interview questions
- Numeric Poker OperationsRippling · PHONE SCREENRippling interview questions
- Reference-Counted Deduplicating File SystemCitadel · PHONE SCREENCitadel interview questions
- Machine Message RouterOpenAI · PHONE SCREENOpenAI interview questions
- String Copy and Move LifecycleSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions
- Versioned Recipe SystemThe Trade Desk · OA
- File System Shell CommandsHarvey · ONSITE INTERVIEWHarvey interview questions
- Parcel Event TrackingAirbnb · OAAirbnb interview questions