PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches

Quick Overview

This question evaluates understanding of tree traversal and depth-first search along with competence in handling hierarchical data structures and constructing full file paths.

  • easy
  • NVIDIA
  • Coding & Algorithms
  • Software Engineer

Return all file paths via DFS

Company: NVIDIA

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Technical Screen

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.txt` - `root/a/c/d.md` - `root/e.log` Implement the function using DFS.

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.

You are given an in-memory file system represented as a tree. Each node is a dictionary with three fields: `name` (string), `isFile` (boolean), and `children` (list of child nodes, empty for files). Write a function that returns all full paths to file nodes in the tree. A full path is built by joining node names with `/` from the root down to each file. Only files should appear in the result, not directories. Use depth-first search (DFS) to traverse the tree. For consistent evaluation, return the paths in DFS preorder, following the order of each node's `children` list from left to right. Special path rule: if the root node's name is `/`, then a child named `etc` should produce `/etc`, not `//etc`.

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

  1. Carry the current path along as you traverse deeper into the tree.
  2. If you use an explicit stack for DFS, push children in reverse order to preserve left-to-right traversal.
Last updated: Apr 19, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Compute the Final Robot Score - NVIDIA (easy)
  • Implement a disk space manager with eviction - NVIDIA (medium)
  • Implement encode/decode for list of strings - NVIDIA (easy)
  • Implement short algorithms on logs, grids, and strings - NVIDIA (hard)
  • Implement matrix transpose and KV store - NVIDIA (medium)