Hard · System Design

Design In-Memory File System

Design a FileSystem over absolute paths with ls(path), which returns [name] for a file path or the directory's entry names in lexicographic order, mkdir(path), which creates the directory and any missing parents, addContentToFile(filePath, content), which creates the file or appends to it, and readContentFromFile(filePath), which returns its content. Given a sequence of calls, return the results of the ls and read calls in order.

Examples

Example 1

{
  "ops": [
    {"kind": "mkdir", "path": "/a/b"},
    {"kind": "add", "path": "/a/b/c", "content": "hello"},
    {"kind": "add", "path": "/a/b/c", "content": " world"},
    {"kind": "ls", "path": "/a/b"},
    {"kind": "read", "path": "/a/b/c"}
  ]
}

Output: [ ["c"], "hello world" ]

Example 2

{
  "ops": [
    {"kind": "mkdir", "path": "/docs/img"},
    {"kind": "add", "path": "/docs/readme", "content": "hi"},
    {"kind": "ls", "path": "/docs/readme"}
  ]
}

Output: [ ["readme"] ]

Rebuild it in the studio

Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.

More System Design problems