KatInterview

Build a Virtual Directory Tree with File Operations

Data Structure DesignAuto-graded · 13 test cases
HardAuto-gradedData Structure Designtreeshash-tablesdata-structure-design

Last updated

Design and implement a tree-based in-memory filesystem supporting directory creation, file I/O, listing, and deletion via absolute paths.

Design an in-memory virtual filesystem that organizes directories and files in a hierarchical tree structure. Your implementation must process a sequence of filesystem commands and return their results.

Implement a function that accepts two parallel lists:

  • operations: command names as strings
  • arguments: corresponding argument lists for each command

The function returns a list of results, one per operation.

Supported Commands

mkdir — arguments: [path]

  • Creates a directory at the given absolute path
  • Returns True if created successfully
  • Returns False if:
    • A file already exists at that path
    • The parent directory does not exist
    • The path is the root directory /

touch — arguments: [path, content]

  • Creates or overwrites a file at the specified path with the given content string
  • Returns None (always)
  • Does nothing if the parent directory is missing or if the path points to an existing directory

read — arguments: [path]

  • Returns the content string of the file at the specified path
  • Returns None if the path does not exist or points to a directory

ls — arguments: [path]

  • Returns a sorted list of entry names within the directory
  • If the path points to a file, returns a single-element list containing only that file's name
  • Returns an empty list if the path does not exist

rm — arguments: [path]

  • Deletes the file or directory at the path (recursively removes all descendants if it's a directory)
  • Returns None (always)
  • Does nothing if the path does not exist or is the root directory

Path Rules

  • All paths are absolute, starting with /
  • The root directory / always exists and cannot be deleted
  • Path components are separated by / and consist of alphanumeric characters, dots, or underscores
  • Multiple consecutive slashes are treated as a single separator (e.g., //home///user equals /home/user)

Constraints

  • 1 ≤ number of operations ≤ 1000
  • 1 ≤ path length ≤ 200
  • 0 ≤ content length ≤ 1000
  • All paths are absolute (start with /)
  • Operation names are valid (one of the five commands above)

Examples

Example 1

Input: virtual_filesystem(['mkdir', 'touch', 'ls', 'read'], [['/data'], ['/data/notes.txt', 'hello world'], ['/data'], ['/data/notes.txt']])

Output: [True, None, ['notes.txt'], 'hello world']

Hints

Hint 1

Model the filesystem as a tree where each node is either a directory (containing a map of children) or a file (containing content).

Hint 2

Parse each path into components by splitting on slashes and filtering empty strings to handle consecutive separators.

Hint 3

For mutations like mkdir and touch, navigate to the parent directory first, then manipulate the target name in the parent's children map.