File System
Implement a data structure with the following methods:
bool create(String path, int content)
which creates a path atpath
if there’s a parent directory andpath
doesn’t exist yet, and sets its value ascontent
. Returns whether it was newly created. Initially only the root directory at"/"
exists.int get(String path)
which returns the value associated withpath
. If there’s no path, return-1
.
Constraints
0 ≤ n ≤ 100,000
wheren
is the number of calls tocreate
andget
https://binarysearch.com/problems/File-System
Examples
Example 1
Input
- methods =
['constructor', 'create', 'create', 'get', 'get', 'create', 'get']
- arguments =
[[], ['/usr', 1], ['/usr/bin', 2], ['/usr'], ['/usr/bin'], ['/proc/1/exe', 3], ['/non/existent']]
Output
- answer =
[None, True, True, 1, 2, False, -1]
Explanation
fs = FileSystem() # Only "/" exists
fs.create("/usr", 1) == True # Now "/" and "/usr" exists
fs.create("/usr/bin", 2) == True# Now "/", "/usr" and "/usr/bin" exists
fs.get("/usr") == 1
fs.get("/usr/bin") == 2
fs.create("/proc/1/exe", 3) == False # Parent dir "/proc/1/" doesn't exist, so can't create here
fs.get("/non/existent") == -1
Leave a comment