LeetCode 1166, Medium. Topics: Design, Trie, Hash Table, String. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 14 parametrized pytest cases, and a playground notebook:
Problem
You are asked to design a file system that allows you to create new paths and associate them with different values.
The format of a path is one or more concatenated strings of the form: / followed by one or more lowercase English letters. For example, '/leetcode' and '/leetcode/problems' are valid paths while an empty string '' and '/' are not.
Implement the FileSystem class:
bool createPath(string path, int value) Creates a new path and associates a value to it if possible and returns true. Returns false if the path already exists or its parent path doesn’t exist.
int get(string path) Returns the value associated with path or returns -1 if the path doesn’t exist.
Examples
Constraints
2 <= path.length <= 100
1 <= value <= 10^9
- Each
path is valid and consists of lowercase English letters and '/'.
- At most
10^4 calls in total will be made to createPath and get.
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
NeetCode All. Last modified on September 7, 2026