Netflix questions

In-Memory File System

NetflixOnsiteMedium

Build a file system that keeps directories and files in memory. Paths are absolute strings starting with / and split on /; / itself is the root directory. Each name between slashes contains only lowercase letters, digits and .. A path never passes through a file, and a name is never used as both a file and a directory.

Each test creates one FileSystem instance and runs a sequence of [method, [args]] calls. The output lists every call's return value, with None rendered as null.

Part 1: Directories and files

Implement a FileSystem with four methods:

  • mkdir(path) creates the directory at path, including any missing parent directories. If the directory already exists, nothing changes.
  • add_content(path, text) appends text to the file at path, creating the file and any missing parent directories if needed.
  • read_content(path) returns the full text stored at the file path. The file always exists.
  • ls(path) returns the names inside the directory at path, sorted in ascending order. If path is a file, it returns a list containing only that file's name. path always exists.

mkdir and add_content return None.

  1. Example 1
    init
    []
    operations
    [["mkdir",["/a/b"]],["add_content",["/a/b/c.txt","hello"]],["add_content",["/a/b/c.txt"," world"]],["read_content",["/a/b/c.txt"]],["ls",["/a"]],["ls",["/a/b"]],["ls",["/a/b/c.txt"]],["ls",["/"]]]
    Output
    [null,null,null,"hello world",["b"],["c.txt"],["c.txt"],["a"]]

    Why: mkdir("/a/b") also creates the missing parent /a. The file receives hello and then world, so reading it returns hello world. Listing a file returns just that file's name.

Constraints

  • 1 <= len(operations) <= 10^4
  • 1 <= len(path) <= 200
  • 1 <= len(text) <= 100
  • text contains only lowercase letters, digits and spaces