279. Design File System With Create, Get and Delete Path
Design File System With Create, Get and Delete Path
Design an in-memory file system that stores a string value at each path. The file system must support creating paths, retrieving stored values, and deleting paths.

Path Rules

  • A valid path contains one or more lowercase English letter components separated by /.
  • Every valid path starts with /.
  • Examples of valid paths are /documents and /documents/images.
  • Examples of invalid paths include "", /, documents, /documents/, /documents//images, and /Documents.
  • The parent of a top-level path such as /documents is the root, which is considered to exist.
  • The root is used only as the parent of top-level paths and cannot be created, retrieved, or deleted.
  • The file system does not distinguish between files and folders. Any path may store a value and may also have child paths.

Methods

Constructor

FileSystem()
Initializes an empty file system.

Create Path

boolean createPath(String path, String value)
Creates path and associates value with it.
  • Return true when the path is created successfully.
  • Return false if path is invalid.
  • Return false if value is exactly "-1".
  • Return false if the path already exists.
  • Return false if the immediate parent path does not exist.
  • Creating a path does not automatically create any missing parent paths.

Get Value

String get(String path)
Returns the string value associated with path.
  • Return "-1" if path is invalid.
  • Return "-1" if the path does not exist.

Delete Path

boolean deletePath(String path)
Deletes path from the file system.
  • Return true if the path exists and is deleted.
  • Return false if path is invalid.
  • Return false if the path does not exist.
  • If the deleted path has child paths, delete all of its descendants.
  • Deleting a path does not affect its parent or sibling paths.

Deterministic Behavior

  • Each existing path stores exactly one string value.
  • Creating an existing path does not replace its current value.
  • After a path is deleted, it does not exist unless it is successfully created again.
  • Deleting a path always deletes its complete subtree.
  • All operations are processed in the order in which they are called.

Constraints

  • At most 10,000 calls will be made to createPath, get, and deletePath combined.
  • For every valid path, 2 ≤ path.length() ≤ 100.
  • 1 ≤ value.length() ≤ 1,000
  • Every component of a valid path contains only lowercase English letters a-z.
  • value may contain printable characters, but it must not be exactly equal to "-1".
  • The value "-1" is reserved to indicate an invalid or nonexistent path.

Examples

Example 1

FileSystem fileSystem = new FileSystem();
fileSystem.createPath(path = "/projects", value = "active") returns true.
fileSystem.get(path = "/projects") returns "active".
The top-level path is created successfully, and its stored value can be retrieved.

Example 2

FileSystem fileSystem = new FileSystem();
fileSystem.createPath(path = "/users", value = "all-users") returns true.
fileSystem.createPath(path = "/users/admin", value = "administrator") returns true.
fileSystem.createPath(path = "/users/admin/settings", value = "enabled") returns true.
fileSystem.get(path = "/users/admin") returns "administrator".
Each nested path is created after its immediate parent exists.

Example 3

FileSystem fileSystem = new FileSystem();
fileSystem.createPath(path = "/music/rock", value = "songs") returns false.
fileSystem.get(path = "/music/rock") returns "-1".
The path cannot be created because its parent path /music does not exist.

Example 4

FileSystem fileSystem = new FileSystem();
fileSystem.createPath(path = "/store", value = "open") returns true.
fileSystem.createPath(path = "/store/orders", value = "pending") returns true.
fileSystem.createPath(path = "/store/orders/recent", value = "five") returns true.
fileSystem.deletePath(path = "/store/orders") returns true.
fileSystem.get(path = "/store/orders") returns "-1".
fileSystem.get(path = "/store/orders/recent") returns "-1".
fileSystem.get(path = "/store") returns "open".
Deleting /store/orders also deletes its descendants, while its parent remains unchanged.

Example 5

FileSystem fileSystem = new FileSystem();
fileSystem.createPath(path = "/logs", value = "first") returns true.
fileSystem.createPath(path = "/logs", value = "second") returns false.
fileSystem.get(path = "/logs") returns "first".
Creating an existing path fails and does not replace its stored value.

Example 6

FileSystem fileSystem = new FileSystem();
fileSystem.createPath(path = "/", value = "root-value") returns false.
fileSystem.get(path = "/") returns "-1".
fileSystem.deletePath(path = "/") returns false.
The root is not a valid path for creation, retrieval, or deletion.

Example 7

FileSystem fileSystem = new FileSystem();
fileSystem.createPath(path = "/reports/", value = "annual") returns false.
fileSystem.get(path = "/reports/") returns "-1".
fileSystem.deletePath(path = "reports") returns false.
The operations fail because the supplied paths are invalid.

Example 8

FileSystem fileSystem = new FileSystem();
fileSystem.createPath(path = "/archive", value = "-1") returns false.
fileSystem.get(path = "/archive") returns "-1".
The path is not created because "-1" is reserved for invalid or nonexistent paths.


Please use Laptop/Desktop or any other large screen to add/edit code.