Design File System - Problem
Design a File System that supports creating hierarchical paths and storing values! ๐Ÿ“

You need to build a file system where you can:
โ€ข Create paths like /leetcode or /leetcode/problems
โ€ข Associate values with each path
โ€ข Retrieve values by path

The catch? You can only create a path if its parent path already exists. For example, you can't create /leetcode/problems unless /leetcode exists first!

Path Format Rules:
โ€ข Must start with /
โ€ข Followed by one or more lowercase letters
โ€ข Can have multiple segments like /a/b/c
โ€ข Empty string "" and lone "/" are invalid

Operations to implement:
โ€ข createPath(path, value) - Returns true if path created successfully, false if path exists or parent doesn't exist
โ€ข get(path) - Returns the value for the path, or -1 if path doesn't exist

Input & Output

example_1.py โ€” Basic Operations
$ Input: fs = FileSystem() fs.createPath("/leet", 1) fs.createPath("/leet/code", 2) fs.get("/leet/code") fs.createPath("/c/d", 1) fs.get("/c")
โ€บ Output: true true 2 false -1
๐Ÿ’ก Note: First two createPath operations succeed because /leet can be created (parent is root) and /leet/code can be created (parent /leet exists). Third operation fails because parent /c doesn't exist. Last get returns -1 because /c doesn't exist.
example_2.py โ€” Duplicate Path
$ Input: fs = FileSystem() fs.createPath("/a", 1) fs.createPath("/a", 2) fs.get("/a")
โ€บ Output: true false 1
๐Ÿ’ก Note: First createPath succeeds and creates /a with value 1. Second createPath fails because /a already exists. The value remains 1 from the first creation.
example_3.py โ€” Deep Hierarchy
$ Input: fs = FileSystem() fs.createPath("/a", 1) fs.createPath("/a/b", 2) fs.createPath("/a/b/c", 3) fs.get("/a/b/c") fs.createPath("/a/x/y", 4)
โ€บ Output: true true true 3 false
๐Ÿ’ก Note: Successfully creates a deep hierarchy /a โ†’ /a/b โ†’ /a/b/c. The last createPath fails because /a/x doesn't exist (can't create /a/x/y without parent /a/x).

Visualization

Tap to expand
File System Design๐Ÿ“Root (/)/leetcodevalue: 100/apivalue: 200/leetcode/problemsvalue: 300/api/usersvalue: 400๐Ÿ’ก Key Insight:Hash map enables O(1) parent validationโšก Performance:Both createPath() and get() are O(1) average
Understanding the Visualization
1
Initialize
Start with empty hash map to store path-value pairs
2
Create Path
Check if parent exists, then add new path to hash map
3
Get Value
Look up path directly in hash map for instant retrieval
Key Takeaway
๐ŸŽฏ Key Insight: Using a hash map transforms this from O(n) linear search to O(1) instant lookup, making it highly scalable for real-world file systems!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(1)

Hash map provides O(1) average lookup, parent path extraction is O(path_length)

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Store n path-value pairs in hash map

n
2n
โšก Linearithmic Space

Constraints

  • The number of calls to the two functions is at most 104 in total
  • 2 โ‰ค path.length โ‰ค 100
  • path will be a valid path that starts with '/' and contains only lowercase English letters
  • 1 โ‰ค value โ‰ค 109
  • At most 104 unique paths will be created
Asked in
Amazon 25 Google 20 Microsoft 15 Meta 12
28.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen