FastPrepHierarchical In-Memory File System

Hierarchical In-Memory File System

Clay logoClay● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Process operations on an in-memory hierarchy rooted at /.

  • MKDIR path recursively creates missing folders and returns true, unless a path component is a file.
  • WRITE path content creates or replaces a file when its parent folder exists and returns whether it succeeded.
  • READ path returns file content or the empty string.
  • LIST path returns direct children sorted as comma-separated D:name or F:name.
  • MOVE source destinationFolder moves a file or folder under an existing folder, preserving its name. Reject root moves, cycles, and collisions.
  • DELETE path removes a file or a folder and all descendants. Root cannot be deleted.

Return one string for every operation.

Function

runHierarchicalFileSystem(operations: String[][]) → String[]

Examples

Example 1

operations = [["MKDIR","/a/b"],["WRITE","/a/b/note","hello"],["READ","/a/b/note"],["LIST","/a/b"],["DELETE","/a"],["READ","/a/b/note"]]return = ["true","true","hello","F:note","true",""]

Recursive creation, file access, listing, and cascading deletion are applied in order.

Example 2

operations = [["MKDIR","/src/pkg"],["MKDIR","/dst"],["WRITE","/src/pkg/a","x"],["MOVE","/src/pkg","/dst"],["LIST","/dst"],["READ","/dst/pkg/a"]]return = ["true","true","true","true","D:pkg","x"]

Moving a folder rewrites every descendant path.

Constraints

  • 1 <= operations.length <= 10000
  • Paths are normalized absolute paths with no trailing slash except /.
  • Names and file contents contain no commas.

More Clay problems

See Clay hiring insights
public String[] runHierarchicalFileSystem(String[][] operations) {
  // Write your code here.
}
operations[["MKDIR","/a/b"],["WRITE","/a/b/note","hello"],["READ","/a/b/note"],["LIST","/a/b"],["DELETE","/a"],["READ","/a/b/note"]]
expected["true", "true", "hello", "F:note", "true", ""]
Checking account…