Please use Laptop/Desktop or any other large screen for the Mock Interview Session.

Design an In-Memory File System



YouTube Video Thumbnail
Link

Watch above sample mock interview video to see how it works.
Login and Buy Premium to Start the Interview



Design Simple File System

Design Simple File System

Problem Statement

Design a basic file system that offers two operations:

  • addPath(route, val): Creates a new path with an associated integer value if it doesn't exist and its parent path exists. Returns true if successful, otherwise false.
  • fetch(route): Returns the integer value linked with the given path. Returns -1 if the path is not found.

A valid path consists of one or more segments starting with a slash / followed by lowercase letters. For example, /example and /example/test are valid paths, while an empty string or / alone are invalid.

Implement these two methods to manage the file system.

Examples

addPath("/x", 5)  // returns true
fetch("/x")       // returns 5
    
addPath("/abc", 3)       // returns true
addPath("/abc/def", 7)   // returns true
fetch("/abc/def")        // returns 7
addPath("/xyz/123", 10)  // returns false (parent path "/xyz" does not exist)
fetch("/xyz")            // returns -1
    

Constraints

  • Number of calls to addPath and fetch will not exceed 12,000 in total.
  • Path length is between 3 and 110 characters inclusive.
  • Value associated with a path is between 1 and 109 inclusive.