KatInterview

Build an In-Memory Hierarchical File System with CRUD Support

Data Structure DesignModel answer
MediumModel answerData Structure Designdesign-dstrees-graphsstrings

Last updated

Design and implement an in-memory key-value store structured as a file system tree supporting create, set, get, and cascade delete operations with path validation.

Design an in-memory hierarchical file system that maps unique string paths to integer values. Your system should store values at specific path nodes and enforce hierarchical structural rules.

Implement the FileSystem class with the following methods:

  • create(path: str, value: int) -> bool: Creates a new path node containing value. Returns true if creation is successful. Returns false if the path format is invalid, if the path already exists, or if its parent path does not exist. (The root directory / exists implicitly at initialization and cannot be explicitly created).
  • get(path: str) -> int: Returns the integer value associated with path. If the path does not exist or is invalid, returns -1.
  • set(path: str, value: int) -> bool: Updates the integer value at an existing path. Returns true if successful. Returns false if the path does not exist or is invalid (does not create non-existent nodes).
  • delete(path: str) -> bool: Removes the specified path and all of its descendants (a recursive directory removal). Returns true if the path existed and was successfully deleted. Returns false if the path does not exist or is invalid.

Path Formatting Rules

A path is considered valid if and only if:

  1. It begins with a single forward slash /.
  2. It is not equal to / itself (the root directory cannot be modified, assigned a value, or deleted).
  3. It contains no trailing slashes (e.g., /foo/ is invalid).
  4. It contains no consecutive slashes (e.g., /foo//bar is invalid).
  5. Each directory/file name segment consists solely of lowercase English letters (a-z).

Examples

fs = FileSystem()

# Step 1: Create top-level path
fs.create("/user", 10)  # returns True

# Step 2: Create nested path under existing parent
fs.create("/user/profile", 20)  # returns True

# Step 3: Attempting to create under non-existent parent fails
fs.create("/admin/settings", 30)  # returns False (parent "/admin" does not exist)

# Step 4: Duplicate path creation fails
fs.create("/user", 15)  # returns False

# Step 5: Fetch values
fs.get("/user/profile")  # returns 20
fs.get("/missing")  # returns -1

# Step 6: Update existing value
fs.set("/user/profile", 25)  # returns True
fs.get("/user/profile")  # returns 25

# Step 7: Cascading delete
fs.delete("/user")  # returns True (deletes "/user" and "/user/profile")
fs.get("/user/profile")  # returns -1

Hints

Hint 1

Consider representing the file system as a segment-based Trie (prefix tree) rather than a flat hash map.

Hint 2

Think about how `delete` behaves in a Trie versus a flat Hash Map: unlinking a single child pointer in a Trie automatically disconnects all descendant nodes in O(depth) time.

Hint 3

Ensure strict validation of path strings before performing structural lookups or operations to prevent partial or invalid state updates.