In-Memory Directory and File Operations
Updated
Problem statement
Implement a small in-memory model of Linux-style directory and file operations. Start with one empty directory at /.
For this exercise, assume each command is [operation, absolutePath]:
mkdircreates one empty directory. Its parent must already be a directory, and the target must not exist.createcreates one empty file under the same parent and new-target rules.lslists the immediate names in a directory, sorted lexicographically and joined by commas. An empty directory returns an empty string. Listing a file returns its final path component.
Return one string per command. A successful creation returns OK. Invalid creation or listing a missing path returns ERROR. Paths are already canonical; do not create missing parents automatically. Directories and files share one namespace, and the root already exists.
Function
fileSystemOperations(commands: String[][]) → String[]Examples
Example 1
commands = [["mkdir","/docs"],["create","/docs/b"],["create","/docs/a"],["ls","/docs"]]return = ["OK","OK","OK","a,b"]The directory contains files named b and a. Listing sorts their names, so its result is a,b.
Example 2
commands = [["create","/a/b"],["ls","/a"],["mkdir","/a"],["create","/a/b"],["ls","/a/b"]]return = ["ERROR","ERROR","OK","OK","b"]The first creation fails because /a is missing. After creating that parent, file creation succeeds, and listing the file returns its name b.
Example 3
commands = [["ls","/"],["mkdir","/"],["create","/"],["mkdir","/x"],["mkdir","/x"]]return = ["","ERROR","ERROR","OK","ERROR"]The root starts empty and already exists. Both attempts to create it fail. The first creation of /x succeeds; the duplicate fails.
Constraints
0 <= commands.length <= 200.- Each command has exactly
2tokens, and every token has length at most200. - The operation is
mkdir,createorls. Each path is/or an absolute sequence of names matching[a-z][a-z0-9]{0,11}, separated by single slashes. Non-root paths have no trailing slash, dot components or empty components.
More Squarepoint Capital interview questions
- Mutable Record Store with Error ResultsONSITE INTERVIEW · Seen Sep 2026
- Maximum SubarrayONSITE INTERVIEW · Seen Aug 2026
- Product of Array Except SelfONSITE INTERVIEW · Seen Aug 2026
- Estimate Pi with a Finite SeriesONSITE INTERVIEW · Seen Oct 2025
- Count C++ Constructor and Assignment CallsONSITE INTERVIEW · Seen Jun 2025
- Observe a Lazy GeneratorONSITE INTERVIEW · Seen Feb 2025
- Order Matching on ExecuteONSITE INTERVIEW · Seen Dec 2024
- Accumulate Numeric Value ObjectsONSITE INTERVIEW · Seen Sep 2024
- Differentiate a PolynomialONSITE INTERVIEW · Seen Aug 2023
- Newton Square Root to a Guaranteed TolerancePHONE SCREEN · Seen Nov 2026
Similar questions at other companies
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- In-Memory Vault File SystemHarvey · ONSITE INTERVIEWHarvey interview questions
- Account Scheduler - Availability and AcquireStripe · ONSITE INTERVIEWStripe interview questions
- In-Memory Spreadsheet ValuesRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
- File System PathsCapital One · 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
- Design File SystemGoogle · PHONE SCREENGoogle interview questions
- Reference-Counted Deduplicating File SystemCitadel · PHONE SCREENCitadel interview questions