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.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Medium

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
String Parsing
Prefix Sums
Hash Map
Tree Serialization
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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...


Submit Your Answer
Markdown supported