404. In-Memory File System with Path Values
Asked in
In-Memory File System with Path Values

Design an in-memory file system that stores directories, files, file content, and integer values associated with selected directory paths.

The file system begins with an empty root directory represented by "/".

Class

FileSystem

Constructor

FileSystem()

Initialize an empty file system containing only the root directory.

Methods

ls

List<String> ls(String path)

Parameters

  • path: The absolute path of an existing file or directory.

Returns

If path identifies a file, return a list containing only that file's name.

If path identifies a directory, return the names of its immediate files and subdirectories in lexicographic order.

mkdir

void mkdir(String path)

Parameters

  • path: The absolute directory path to create.

Create the directory and every missing intermediate directory in the path. Directories created by this method do not have associated integer values.

addContentToFile

void addContentToFile(String filePath, String content)

Parameters

  • filePath: The absolute path of the file.
  • content: The text to add to the file.

Create the file with the supplied content when it does not exist. Otherwise, append the content to the end of the existing file content.

readContentFromFile

String readContentFromFile(String filePath)

Parameters

  • filePath: The absolute path of an existing file.

Returns

Return the complete content stored in the file.

createPath

boolean createPath(String path, int value)

Parameters

  • path: The new absolute directory path to create.
  • value: The integer value to associate with the path.

Returns

Create the path as a directory with the associated value and return true when the path does not already exist and its immediate parent directory exists.

Return false when the path already exists, its parent does not exist, or its parent is a file. Unlike mkdir, this method does not create missing parent directories.

get

int get(String path)

Parameters

  • path: The absolute path whose associated value is requested.

Returns

Return the integer value associated with the path. Return -1 if the path does not exist or was created without an associated value.

Rules

  • Every path is absolute, begins with "/", and does not end with "/", except for the root path "/".
  • The root directory always exists and cannot be created using createPath.
  • File and directory names contain only lowercase English letters.
  • A file and a directory cannot have the same name inside the same parent directory.
  • A path successfully created by createPath is a directory, can contain other entries, and appears in directory listings.
  • Calling mkdir for an existing directory has no effect. It does not remove or change an existing associated value.
  • Calling mkdir does not assign values to the directories it creates.
  • No path passed to mkdir has a file as one of its path components.
  • The parent directory of a file passed to addContentToFile already exists.
  • A path passed to addContentToFile never identifies an existing directory.
  • Paths passed to ls exist, and paths passed to readContentFromFile identify existing files.

Constraints

  • At most 10,000 method calls will be made.
  • 2 ≤ path.length() ≤ 100 for every path other than "/".
  • 2 ≤ filePath.length() ≤ 100
  • 1 ≤ content.length() ≤ 50
  • The total stored file content will not exceed 200,000 characters.
  • 1 ≤ value ≤ 1,000,000,000

Examples

Example 1

FileSystem fileSystem = new FileSystem()

fileSystem.ls(path = "/") returns [] because the root directory is initially empty.

fileSystem.mkdir(path = "/projects/api") creates both directories.

fileSystem.createPath(path = "/projects/api/cache", value = 17) returns true because its parent directory exists.

fileSystem.createPath(path = "/projects/web/ui", value = 25) returns false because the immediate parent path "/projects/web" does not exist.

fileSystem.get(path = "/projects/api/cache") returns 17.

fileSystem.get(path = "/projects/api") returns -1 because the directory was created without an associated value.

fileSystem.addContentToFile(filePath = "/projects/api/readme", content = "hello") creates the file.

fileSystem.addContentToFile(filePath = "/projects/api/readme", content = " world") appends text to the file.

fileSystem.ls(path = "/projects/api") returns ["cache", "readme"].

fileSystem.readContentFromFile(filePath = "/projects/api/readme") returns "hello world".

Example 2

FileSystem fileSystem = new FileSystem()

fileSystem.createPath(path = "/data", value = 7) returns true.

fileSystem.createPath(path = "/data/logs", value = 11) returns true.

fileSystem.createPath(path = "/data", value = 20) returns false because the path already exists.

fileSystem.mkdir(path = "/data/archive/old") creates the missing directory structure.

fileSystem.addContentToFile(filePath = "/data/archive/note", content = "saved") creates a new file.

fileSystem.ls(path = "/data") returns ["archive", "logs"].

fileSystem.ls(path = "/data/archive/note") returns ["note"].

fileSystem.get(path = "/data/logs") returns 11.

fileSystem.get(path = "/data/archive") returns -1 because that directory has no associated value.

fileSystem.get(path = "/missing") returns -1 because the path does not exist.



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