← Questions
List every file path in a directory tree
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Nvidia's technical phone screen, last asked Mar 2026. It tests dfs, trees and file systems.
You get an in-memory file system as a tree. Each node has a name, an isFile flag and a list of children (empty for files). Return the full path of every file, joining names with / from the root down. Directories do not appear in the output.
Interface
Unless you agree on a different interface with the interviewer, assume:
-
file_paths(root: dict | None) -> list[str]returns paths in DFS pre-order, following each node'schildrenfrom left to right;[]for an empty tree. - If the root's name is
/, a childetcgives/etc, not//etc.
Example
root
├── a
│ ├── b.txt
│ └── c
│ └── d.md
└── e.log
file_paths(root) -> ["root/a/b.txt", "root/a/c/d.md", "root/e.log"]
file_paths(None) -> []
Constraints
- Up to 10^5 nodes; trees can be very deep.
- Names are non-empty strings.
Clarifying questions
- Does the order of the output matter?
- How is the root named, and does it appear in the paths?
- Can a directory be empty, and should it appear?
- How deep can the tree be?
What a strong answer covers
- A recursive DFS that carries the current path (or a list of segments joined at files), producing pre-order output.
- An iterative version with an explicit stack that pushes children in reverse to keep left-to-right order, for trees deeper than the recursion limit.
- Cost:
O(total output length), since every path is written out. - Edge cases: empty tree, a root that is itself a file, empty directories, the
/root.
Follow-ups
- Return only files matching a pattern such as
*.log. - Return the total size per directory.
- Walk a real file system that may contain symbolic links that form cycles.
Where it's asked
- Nvidia · Technical phone screen · Last asked Mar 2026Live technical interview, 45 to 90 minutes; a project discussion, then a small task
What it tests
dfs · trees · file systems
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
