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
โข 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
Path Format Rules:
โข Must start with
โข Followed by one or more lowercase letters
โข Can have multiple segments like
โข Empty string
Operations to implement:
โข
โข
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 invalidOperations 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
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)
โ Linear Growth
Space Complexity
O(n)
Store n path-value pairs in hash map
โก 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code