Return all file paths via DFS
Company: NVIDIA
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of tree traversal and depth-first search along with competence in handling hierarchical data structures and constructing full file paths.
Constraints
- The total number of nodes is between 0 and 100000.
- Each file node has an empty `children` list.
- Node names are non-empty strings; the root name may be `/` or any directory name such as `root`.
Examples
Input: {'name': 'root', 'isFile': False, 'children': [{'name': 'a', 'isFile': False, 'children': [{'name': 'b.txt', 'isFile': True, 'children': []}, {'name': 'c', 'isFile': False, 'children': [{'name': 'd.md', 'isFile': True, 'children': []}]}]}, {'name': 'e.log', 'isFile': True, 'children': []}]}
Expected Output: ['root/a/b.txt', 'root/a/c/d.md', 'root/e.log']
Explanation: DFS visits `a` before `e.log`, then `b.txt`, then `c/d.md`, so the file paths are returned in that order.
Input: None
Expected Output: []
Explanation: An empty file system has no file paths.
Input: {'name': 'root', 'isFile': False, 'children': []}
Expected Output: []
Explanation: The root directory contains no files.
Input: {'name': '/', 'isFile': False, 'children': [{'name': 'etc', 'isFile': False, 'children': [{'name': 'config.yaml', 'isFile': True, 'children': []}]}, {'name': 'readme.md', 'isFile': True, 'children': []}]}
Expected Output: ['/etc/config.yaml', '/readme.md']
Explanation: When the root is `/`, child paths should start with a single slash, not a double slash.
Input: {'name': 'notes.txt', 'isFile': True, 'children': []}
Expected Output: ['notes.txt']
Explanation: If the root itself is a file, its full path is just its own name.
Hints
- Carry the current path along as you traverse deeper into the tree.
- If you use an explicit stack for DFS, push children in reverse order to preserve left-to-right traversal.