We use cookies for site analytics. Accept to help us understand how the site is used. See our Privacy Policy for details.
A hard design problem, graded against 4 test cases (1 of them hidden).
Build a working data structure to an API - LRU caches, rate limiters, and iterators.
Reach for it when you see: "Design a class supporting these operations in O(1)" - an API rather than a single function.
More Designproblems →Each node has:
- `children`: `Map<name, Node>` (sub-directories and files)
- `content`: `string` (only used when this node is a file)
- `isFile`: boolean
Walk the path components from root, creating intermediate dirs in mkdir / addContentToFile if missing.
ls(path): traverse to the node. If it's a file, return `[lastComponent]`. If it's a dir, return its child names sorted.
Edge cases:
- `ls("/")` on an empty fs returns `[]`.
- `addContentToFile` to an existing file appends - don't overwrite.
- The last component on `addContentToFile` is the file name, not a dir.
The full reference solution in every supported language stays in the editor above - reveal it there once you have had a real attempt.
These apply to the pattern as a whole, not just this problem.