← Uber Interview Insights

Uber·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
May 2026

Summary

Uber coding interview with a filesystem traversal problem. Pretty straightforward if you've done DFS before, but the output formatting tripped me up for a minute.

Questions Asked (1)

Q1

Given the root of a directory structure, print every file and subdirectory with indentation arrows indicating depth, similar to the Unix tree command. Implement it using DFS.

Algorithms & Data StructuresSystem Design
Author's notes

The DFS part clicked pretty fast.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem: define the input (root node of a directory tree) and output (indented string representation). Then, implement a recursive DFS that traverses each directory, printing its name with appropriate indentation before recursing into its children. Finally, discuss edge cases and potential optimizations.

Pro tip: Mention that you would use a depth parameter to control indentation and that you'd handle both files and directories uniformly, but only recurse into directories. Also, note that you'd avoid using string concatenation in a loop for performance, opting for a list of strings or a StringBuilder.

1. Clarify requirements and assumptions

Confirm the input format (e.g., a Node class with name, isDirectory, and children) and output format (e.g., string with indentation). Ask about sorting order, handling of hidden files, and whether to include the root.

2. Design the DFS algorithm

Outline a recursive function that takes a node and depth. Print the node's name with indentation based on depth, then if it's a directory, iterate over its children and recursively call the function with depth+1.

3. Implement the solution

Write clean code, using a helper function for indentation (e.g., repeating ' ' or '|-- '). Ensure base case: if node is null, return. Use a list to collect lines for efficiency.

4. Test with examples and edge cases

Walk through a simple tree, then test edge cases: empty directory, single file, deep nesting, and large tree. Discuss time and space complexity: O(n) time, O(h) space for recursion stack.

5. Discuss optimizations and variations

Mention iterative DFS with a stack if recursion depth is a concern, or BFS if level-order is needed. Also, discuss handling symbolic links or permission errors.

Key Points to Mention

  • Use of recursion for DFS, with depth parameter for indentation.
  • Time complexity O(n) where n is number of nodes, space O(h) for recursion stack.
  • Handling both files and directories: only recurse into directories.
  • Efficiency: avoid string concatenation in loops; use list or StringBuilder.
  • Edge cases: empty tree, single node, deep recursion, and non-ASCII names.
  • Potential follow-up: iterative DFS using explicit stack, or BFS for level-order printing.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.