← Apple Interview Insights

Apple·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jul 2026

Summary

Apple SWE coding round with a filesystem path problem. The hint about using a stack was actually embedded in the problem itself which felt a little generous, but the implementation details still tripped me up.

Questions Asked (1)

Q1

Given a string representation of a file system where entries are separated by newlines and tab characters indicate directory depth, return the length of the longest absolute path to a file. Return 0 if no files exist.

Algorithms & Data Structures
Author's notes

I parsed the depth fine using the tab count but then fumbled on tracking cumulative path lengths per depth level.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a stack to maintain the cumulative path length at each directory depth. For each line, compute its depth by counting leading tabs, then update the stack: if the line is a file, calculate the total path length; if it's a directory, push the new cumulative length. Keep track of the maximum file path length seen.

Pro tip: Clarify that the path length includes the '/' separators between components, and that only files (not directories) count. Also, mention that you assume the input is well-formed (e.g., no empty lines) but can handle edge cases like no files.

1. Parse the input

Split the string by newline characters to get each entry. For each entry, determine its depth by counting the number of leading tab characters.

2. Maintain a stack of cumulative lengths

Use a stack where each element represents the cumulative length of the path up to that depth. When processing a line at depth d, ensure the stack size is d+1 (pop extra elements if needed).

3. Update stack and track max

For a directory, push the new cumulative length (previous length + name length + 1 for '/'). For a file, compute the total length (previous length + name length) and update the maximum if larger.

4. Return the result

After processing all lines, return the maximum length found, or 0 if no files were encountered.

Key Points to Mention

  • Depth is determined by the number of leading tabs.
  • Cumulative path length includes '/' separators between components.
  • Only files (not directories) contribute to the answer.
  • Use a stack to efficiently track the current path lengths at each depth.
  • Edge cases: no files present, very deep nesting, long file names.
  • Time complexity O(n) where n is total characters, space O(d) where d is maximum depth.

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