Skip to content

Build a Directory Tree from File Paths

Published 3 October 2026

Coding — Build a Directory Tree from File Paths

The coding portion provided a collection of absolute filesystem paths. For example, a rewritten input could look like:

paths = [
  "/workspace",
  "/workspace/docs",
  "/workspace/docs/spec.pdf",
  "/workspace/src",
  "/workspace/src/api",
  "/archive",
  "/archive/docs",
  "/archive/docs/notes.txt"
]

The task was to use these paths to construct the corresponding hierarchical directory structure. Conceptually, the resulting tree would contain structures such as:

/workspace
  /docs
    /spec.pdf
  /src
    /api

/archive
  /docs
    /notes.txt

Directory names were not globally unique. For example, both /workspace/docs and /archive/docs are valid because the complete paths are different. Complete paths, however, were guaranteed to be unique. The system also needed to support searching the tree for a file and returning that file's complete path.

Follow-Up — Very Long Paths

My initial representation treated each complete path as information associated with a tree node. The interviewer then asked: What happens if individual filesystem paths become extremely long? That follow-up changed the expected representation so that tree nodes should correspond more naturally to individual path components rather than repeatedly storing complete path strings.

Follow-Up — No Recursion

The final requirement was to perform the required tree traversal without using recursion. Instead, I was expected to maintain the traversal state explicitly with a stack. I understood the requirement but spent too much time working through the iterative traversal implementation and did not finish before the interview ended.