Traverse a path and print directory tree
Directory Tree Traversal and Listing
You are tasked with writing a Node.js function or program that processes a given file-system path, rootPath, which can be either absolute or relative.
Core Functionality
- Path Validation: The function must first validate that the provided
rootPathactually exists on the file system. - Content Reading: It should then read the contents located under
rootPath. - Output Generation: Finally, it must output a listing that includes both files and subfolders found.
Output Format
You must choose one of the following output formats and clearly document your choice in your solution:
- Tree Format: An indented text representation, similar to the
treecommand.root/ a.txt sub/ b.js - Structured JSON: A JSON object representation, typically structured as
{ name, type, children }.
Requirements and Edge Cases
- File as
rootPath: IfrootPathpoints directly to a file, the output should simply be that file's entry. - Directory as
rootPath: IfrootPathpoints to a directory, the output must include all its immediate children. You have the option to implement full recursive traversal; if you do, clearly state this in your solution. - Error Handling: Gracefully handle common file system errors, such as a non-existent path or permission denied issues.
- Sorting: If you choose to sort the entries within a directory, ensure the sort order is deterministic and clearly specify the chosen criteria (e.g., folders first, then lexicographic by name).
AI interview prep
Core dilemma
Balancing robust, asynchronous file system I/O operations with efficient memory management and comprehensive error handling, especially when dealing with potentially large or deep directory structures, while ensuring a clear and consistent output.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Traverse a path and print directory tree
Directory Tree Traversal and Listing
You are tasked with writing a Node.js function or program that processes a given file-system path, rootPath, which can be either absolute or relative.
Core Functionality
- Path Validation: The function must first validate that the provided
rootPathactually exists on the file system. - Content Reading: It should then read the contents located under
rootPath. - Output Generation: Finally, it must output a listing that includes both files and subfolders found.
Output Format
You must choose one of the following output formats and clearly document your choice in your solution:
- Tree Format: An indented text representation, similar to the
treecommand.root/ a.txt sub/ b.js - Structured JSON: A JSON object representation, typically structured as
{ name, type, children }.
Requirements and Edge Cases
- File as
rootPath: IfrootPathpoints directly to a file, the output should simply be that file's entry. - Directory as
rootPath: IfrootPathpoints to a directory, the output must include all its immediate children. You have the option to implement full recursive traversal; if you do, clearly state this in your solution. - Error Handling: Gracefully handle common file system errors, such as a non-existent path or permission denied issues.
- Sorting: If you choose to sort the entries within a directory, ensure the sort order is deterministic and clearly specify the chosen criteria (e.g., folders first, then lexicographic by name).