KatInterview

Simulate a Tiny Hierarchical File Store from a Command Log

Data Structure DesignAuto-graded · 16 test cases

Last updated

Replay a command log against an in-memory directory tree, with auto-created parent folders, file appends, reads and sorted listings.

Simulate a Tiny Hierarchical File Store from a Command Log

You are given a log of commands to replay against a file store that lives entirely in memory. The store is a tree of folders and files, rooted at /. Write a function that executes the commands in order and returns the result of each one.

Input

  • commands: a list of command names.
  • command_args: a list of argument lists. command_args[i] holds the arguments for commands[i].

Both lists have the same length.

Commands

CommandArgumentsWhat it doesResult recorded
"FileSystem"noneResets the store to a single empty root folder /.None
"mkdir"[path]Creates the folder at path. Any missing folders along the way are created too. If the folder already exists, nothing changes.None
"add_content_to_file"[file_path, content]If the file does not exist, creates it (and any missing parent folders) with content. If it already exists, appends content to the end of its current text.None
"read_content_from_file"[file_path]Reads the file's full text.the text, as a string
"ls"[path]If path is a file, lists just that file's own name. If path is a folder, lists the names of everything directly inside it.a list of strings

Listing rules

  • For a file, ls returns a one-element list holding the last component of the path.
  • For a folder, ls returns the immediate children (files and sub-folders), sorted in ascending lexicographic (alphabetical) order.
  • Only direct children appear. Nothing nested deeper is included.
  • An empty folder yields []. This includes the root of a fresh store.

Path rules

  • Every path is absolute and starts with /. It never ends with /, except for the root path / itself.
  • Each component is made of lowercase English letters.
  • Two entries in the same folder never share a name. Entries in different folders may.
  • An empty folder and a file whose text is empty are different things. Remember which entries are files, and do not infer it from their contents.

Output

Return a list with one entry per command, in the same order as commands. Use None for commands that produce no value.

You may assume that every command is valid. For example, you will never read a folder as if it were a file, and you will never list a path that does not exist.

Constraints

  • 1 <= len(commands) == len(command_args) <= 500
  • Every path has length at most 100.
  • Path components consist only of lowercase letters a-z.
  • Every ls and read_content_from_file command targets an existing path of the appropriate kind.
  • The first command is "FileSystem".

Examples

Example 1

Input: simulate_file_store(['FileSystem', 'ls', 'mkdir', 'add_content_to_file', 'ls', 'ls', 'ls', 'read_content_from_file', 'add_content_to_file', 'read_content_from_file'], [[], ['/'], ['/a/b/c'], ['/a/b/c/d', 'hello'], ['/'], ['/a/b'], ['/a/b/c/d'], ['/a/b/c/d'], ['/a/b/c/d', ' world'], ['/a/b/c/d']])

Output: [None, [], None, None, ['a'], ['c'], ['d'], 'hello', None, 'hello world']

Hints

Hint 1

Model the store as a tree. Each node needs a dictionary of children, a flag saying whether it is a file, and a text field. The flag is what separates an empty file from an empty folder.

Hint 2

Split each path on '/' and throw away empty pieces. The root path then becomes an empty list of components, so it needs no special case. One helper can walk the components, creating missing nodes when asked to.

Hint 3

Only 'ls' needs ordering, so keep children in a plain dict and sort the names when listing. For a file, return the last path component instead of children.