PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates filesystem traversal and recursive/iterative algorithm skills, including handling different path types, edge cases such as missing or inaccessible paths, and robustness concerns like symlink cycles when enumerating files.

  • medium
  • Dropbox
  • Coding & Algorithms
  • Software Engineer

Return all files under a path

Company: Dropbox

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are building a simple file crawler. ## Task Implement an API/function: - **Input:** a filesystem path `rootPath` - **Output:** a list/array of **all file paths** contained under `rootPath` (recursively), in any order. Assume: - Paths can be directories or files. - You are given a helper function similar to: - `listChildren(path) -> (subdirs, files)` where `subdirs` are immediate child directories and `files` are immediate child files. ## Notes / edge cases to clarify in your solution - If `rootPath` is a file, return just `[rootPath]`. - Decide what to do if the path does not exist or is not accessible (e.g., throw an error vs return empty). - Avoid infinite loops if the filesystem can contain symlinks (state your assumption if you ignore symlinks).

Quick Answer: This question evaluates filesystem traversal and recursive/iterative algorithm skills, including handling different path types, edge cases such as missing or inaccessible paths, and robustness concerns like symlink cycles when enumerating files.

You are building a simple file crawler. Given a filesystem and a starting path `rootPath`, return a list of **all file paths** contained under `rootPath`, recursively (in any order — return them sorted for determinism). Because a real filesystem isn't available inside this console, the filesystem is provided to your function as a map `fs` that stands in for the interview's `listChildren(path)` helper: - `fs` maps each **directory** path to a pair `[subdirs, files]`, where: - `subdirs` = the list of immediate child **directory** paths, and - `files` = the list of immediate child **file** paths. - A path that is **not** a key of `fs` but is referenced by some directory's `files` (or `subdirs`) is a **file** (or a directory with no children). Implement `findFiles(fs, rootPath)`: - If `rootPath` is a **file** (it exists but is not a directory key), return `[rootPath]`. - If `rootPath` **does not exist** at all (not a directory key and not referenced anywhere), return `[]` (treat as not accessible / empty). - **Symlinks/cycles:** ignore symlinks. Guard against cycles with a visited set so a malformed filesystem (e.g. a directory that lists itself) does not loop forever. Return the collected file paths **sorted**.

Constraints

  • fs maps each directory path to a pair (subdirs, files) of immediate children.
  • Paths are unique strings; the same path is not both a file and a directory.
  • The filesystem may be malformed with cycles (a directory listing itself or an ancestor); the traversal must terminate.
  • rootPath may be a directory, a file, or absent from the filesystem.
  • Return the file paths sorted (any order is acceptable conceptually; sorting makes the result deterministic).

Examples

Input: ({'/': (['/a', '/b'], ['/root.txt']), '/a': ([], ['/a/x.txt', '/a/y.txt']), '/b': (['/b/c'], ['/b/z.txt']), '/b/c': ([], ['/b/c/deep.txt'])}, '/')

Expected Output: ['/a/x.txt', '/a/y.txt', '/b/c/deep.txt', '/b/z.txt', '/root.txt']

Explanation: Full recursive crawl from root: collects /root.txt, the two files under /a, /b/z.txt, and the deeply nested /b/c/deep.txt — 5 files total, returned sorted.

Input: ({'/': (['/a'], ['/r.txt']), '/a': ([], ['/a/1.txt'])}, '/a')

Expected Output: ['/a/1.txt']

Explanation: rootPath is a subdirectory, not the top. Only files under /a are returned; /r.txt (a sibling, above /a) is excluded.

Input: ({'/': (['/a'], ['/file.txt']), '/a': ([], [])}, '/file.txt')

Expected Output: ['/file.txt']

Explanation: rootPath is a file (it appears in /'s files list but is not itself a directory key), so the result is just [rootPath].

Input: ({'/empty': ([], [])}, '/empty')

Expected Output: []

Explanation: rootPath is a directory with no subdirs and no files, so no file paths are collected.

Input: ({'/': (['/a'], []), '/a': ([], ['/a/only.txt'])}, '/missing')

Expected Output: []

Explanation: rootPath '/missing' does not exist anywhere in the filesystem (not a directory key, not referenced), so the function returns [] (not accessible).

Input: ({'/': (['/loop'], ['/top.txt']), '/loop': (['/loop'], ['/loop/inner.txt'])}, '/')

Expected Output: ['/loop/inner.txt', '/top.txt']

Explanation: The directory /loop lists itself as a child (a symlink-style cycle). The visited set prevents re-expanding it, so traversal terminates and each file is collected exactly once.

Hints

  1. Treat the filesystem as a graph: do an iterative DFS/BFS starting at rootPath, expanding each directory's subdirs and collecting its files.
  2. Distinguish the three root cases first: not in the filesystem at all -> []; present but not a directory key -> [rootPath] (it's a file); otherwise traverse.
  3. Keep a `visited` set of directories you have already expanded so a symlink cycle (a directory that lists itself or an ancestor) cannot loop forever.
Last updated: Jun 26, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ 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 worst-case guesses for adaptive hangman - Dropbox (medium)
  • Build a hit/miss word guessing game - Dropbox (medium)
  • Implement feedback for word guessing game - Dropbox (medium)
  • Implement hierarchical folder access check - Dropbox (medium)
  • Review checkout code for defects and privacy - Dropbox (Medium)