Return all file paths via DFS
NVIDIASoftware Engineer · Technical ScreenLast updated
This question evaluates understanding of tree traversal and depth-first search along with competence in handling hierarchical data structures and constructing full file paths.
You are given an in-memory representation of a file system as a tree.
Each node has:
name(string)isFile(boolean)children(list of nodes, empty for files)
The root node represents the root directory (e.g., name "/" or "root").
Write a function that returns all full paths to files in the file system. Paths should be constructed by joining directory names with /.
Notes:
- Only files should appear in the output (not directories).
- The order of returned paths does not matter.
Example:
- root
- a
- b.txt (file)
- c
- d.md (file)
- e.log (file)
Output could be:
root/a/b.txtroot/a/c/d.mdroot/e.log
Implement the function using DFS.
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.