Longest Absolute File Path in a Tab-Indented Directory Listing
Last updated: November 29, 2026
Quick Overview
Given a directory tree serialized as text, where each line is a file or folder name and its depth is the number of leading tabs, return the length of the longest absolute path to a file (a name containing a dot), or 0 if there are no files.
0
0
0 solved
Given a directory tree serialized as text, where each line is a file or folder name and its depth is the number of leading tabs, return the length of the longest absolute path to a file (a name containing a dot), or 0 if there are no files.
A long-running Google phone screen problem that rewards turning indentation into a depth-indexed prefix sum. The interviewer checks one-pass parsing, the slash accounting, and that you do not build full path strings unnecessarily.
What the Interviewer Expects
- Derives each line's depth from its leading tabs
- Stores the path length at each depth instead of full path strings
- Counts the slash separators correctly
- Only files update the answer; directories update the depth map
- Explains why stale deeper entries never cause wrong answers
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Return the path itself, not just its length.
- Indentation uses four spaces instead of tabs, possibly mixed; how do you parse it?
- Compute the total size per directory if each file line ends with a size.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: a string like
"dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext". - Depth = number of leading tab characters. A name with a
.is a file; others are directories. - Path length counts the names plus...
High-Level Design
Lines arrive in pre-order, so when we see a line at depth d, its parent is the most recent directory at depth d - 1. Keep depth_len[d]: the length of the path prefix that a child at depth d wo...