Design a constrained in-memory file system
HarveySoftware Engineer · Technical ScreenLast updated
This question evaluates design and implementation skills for hierarchical in-memory data structures, string parsing and file-name collision handling under constraints, and the ability to reason about time and space complexity.
Design an in-memory hierarchical file system. Implement:
- addFile(String path): Given a UNIX-style absolute path like "/path/to/somewhere/file.txt", create intermediate directories as needed and store a file at the final segment.
- listEntries(String path): Given a directory path, return the names of its immediate children in lexicographic order. For example, after addFile("/path/to/somewhere/file.txt"), listEntries("/path/to/somewhere") -> ["file.txt"], and listEntries("/path/to") -> ["somewhere"]. Constraints: • Each directory may contain at most 5 entries (files or folders). If an insertion would exceed this limit, reject the operation and leave the structure unchanged. • On duplicate name insertion within the same directory, automatically rename the new entry by appending "(k)" before the file extension (if any), where k is the smallest positive integer that yields an unused name. Example sequence in the same directory: addFile("file.txt"), addFile("file.txt"), addFile("file ( 1).txt") results in files named "file.txt", "file ( 1).txt", "file ( 1)( 1).txt". Requirements: Describe your data structures, parsing/extension handling, and the time/space complexity of addFile and listEntries. Discuss edge cases (root path, trailing slashes, invalid paths).
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.