Implement a Hierarchical File System
Company: Harvey
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of hierarchical data modeling and path-based mapping, exercising skills in tree-like data structures, associative maps, and string path handling.
Constraints
- 1 <= len(operations) <= 10^4
- operations[0] == 'FileSystem'
- 1 <= len(path) <= 100
- 1 <= value <= 10^9
- Paths may be invalid and must be handled according to the rules
Examples
Input: (['FileSystem', 'createPath', 'get', 'createPath', 'get', 'createPath', 'createPath', 'get'], [[], ['/a', 1], ['/a'], ['/a/b', 2], ['/a/b'], ['/c/d', 3], ['/a', 4], ['/missing']])
Expected Output: [None, True, 1, True, 2, False, False, -1]
Explanation: Creates /a and /a/b successfully, fails to create /c/d because /c does not exist, fails to recreate /a, and returns -1 for a missing path.
Input: (['FileSystem', 'createPath', 'createPath', 'get', 'createPath', 'get'], [[], ['/', 1], ['/ab/', 2], ['/'], ['/x', 5], ['/x']])
Expected Output: [None, False, False, -1, True, 5]
Explanation: The root cannot be created, a trailing slash makes '/ab/' invalid, get('/') returns -1 because root has no value, and /x is created normally.
Input: (['FileSystem', 'createPath', 'createPath', 'createPath', 'createPath', 'get', 'get'], [[], ['/a', 1], ['/a//b', 2], ['/A', 3], ['/a/c', 4], ['/a/c'], ['/A']])
Expected Output: [None, True, False, False, True, 4, -1]
Explanation: Double slashes create an empty component, uppercase letters are invalid, and a valid child '/a/c' can still be created under existing '/a'.
Input: (['FileSystem', 'createPath', 'createPath', 'createPath', 'get', 'get'], [[], ['/projects/demo', 7], ['/projects', 5], ['/projects/demo', 7], ['/projects'], ['/projects/demo']])
Expected Output: [None, False, True, True, 5, 7]
Explanation: A nested path cannot be created before its parent. After creating '/projects', creating '/projects/demo' succeeds.
Hints
- You do not need a full tree traversal for this API. Storing each complete path in a hash map is enough.
- To create a path like '/a/b/c', only the immediate parent '/a/b' needs to exist. You can find it using the last '/'.