Problem · Design

In-Memory Filesystem Commands

Learn this problem
MediumPerplexity AI logoPerplexity AIFULLTIMEPHONE SCREEN

Problem statement

Process a sequence of commands against an in-memory filesystem. The root directory / exists initially, and the current directory starts at the root. Paths beginning with / are absolute; every other path is resolved beneath the current directory.

  • ["MKDIR", path] creates one empty directory when its parent directory exists and the path is unused.
  • ["TOUCH", path] creates one file under an existing directory when the path is unused.
  • ["LS", path] lists the immediate child names of an existing directory in lexicographic order, joined by commas.
  • ["RM", path] removes an existing file.
  • ["RMDIR", path] removes an existing empty directory unless it is the root, the current directory, or an ancestor of the current directory.
  • ["CD", path] changes the current directory when the path identifies an existing directory.

Return one string per command. A successful mutation or directory change returns "true", while a rejected one returns "false". A failed LS returns "NULL"; listing an empty directory returns the empty string.

Function

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

Examples

Example 1

operations = [["MKDIR","/docs"],["TOUCH","/docs/readme"],["CD","/docs"],["MKDIR","drafts"],["TOUCH","drafts/plan"],["LS","/docs"],["CD","drafts"],["RM","plan"],["RMDIR","/docs"],["CD","/"],["RMDIR","/docs/drafts"],["LS","/docs"]]return = ["true","true","true","true","true","drafts,readme","true","true","false","true","true","readme"]

Relative paths use the current directory. An ancestor of the current directory cannot be removed, but the empty drafts directory can be removed after returning to the root.

Example 2

operations = [["MKDIR","/b"],["MKDIR","/a"],["TOUCH","/a/z"],["TOUCH","/a/m"],["LS","/"],["LS","/a"],["LS","/a/z"],["CD","/missing"],["RM","/a"],["RMDIR","/"]]return = ["true","true","true","true","a,b","m,z","NULL","false","false","false"]

Listings are lexicographic, LS on a file returns NULL, and commands reject targets of the wrong type.

Constraints

  • 1 <= operations.length <= 100000
  • Every operation has exactly one documented shape.
  • A path is either / or a canonical absolute or relative path. Non-root paths contain one or more lowercase ASCII letter, digit, or underscore components separated by single slashes, with no leading slash for relative paths and no trailing slash.
  • Paths do not contain . or .. components.
  • Each path has at most 100 components and at most 1000 characters.
  • The total number of characters across all operations is at most 1000000.
  • The total number of child names emitted by all successful LS commands is at most 200000.

More Perplexity AI problems

drafts saved locally
public String[] runFileSystem(String[][] operations) {
    // Write your code here
}
operations[["MKDIR","/docs"],["TOUCH","/docs/readme"],["CD","/docs"],["MKDIR","drafts"],["TOUCH","drafts/plan"],["LS","/docs"],["CD","drafts"],["RM","plan"],["RMDIR","/docs"],["CD","/"],["RMDIR","/docs/drafts"],["LS","/docs"]]
expected["true", "true", "true", "true", "true", "drafts,readme", "true", "true", "false", "true", "true", "readme"]
checking account