FastPrepIn-Memory Directory and File Operations

In-Memory Directory and File Operations

Squarepoint Capital logoSquarepoint Capital● MediumNEW GRADONSITE INTERVIEW

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]:

  • mkdir creates one empty directory. Its parent must already be a directory, and the target must not exist.
  • create creates one empty file under the same parent and new-target rules.
  • ls lists 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 2 tokens, and every token has length at most 200.
  • The operation is mkdir, create or ls. 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

Similar questions at other companies

Interview reports that mention this question

Squarepoint Capital interview experiences

All Squarepoint Capital interview questionsSee Squarepoint Capital hiring insights
Saved
class Solution {
    public String[] fileSystemOperations(String[][] commands) {
        // write your code here
    }
}
Checking account…