Longest Absolute File Path - Problem

Suppose we have a file system that stores both files and directories. Given a string input representing the file system in a specific format, return the length of the longest absolute path to a file in the abstracted file system.

The input string uses \n (newline) to separate different files/directories, and \t (tab) characters to represent the depth level in the directory hierarchy.

Each file name is of the form name.extension, while directory names consist of letters, digits, and/or spaces without extensions.

For example:

"dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"

represents the directory structure where file2.ext has the absolute path "dir/subdir2/subsubdir2/file2.ext" with length 32.

If there is no file in the system, return 0.

Input & Output

Example 1 — Basic Directory Structure
$ Input: input = "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"
Output: 32
💡 Note: The longest path is "dir/subdir2/subsubdir2/file2.ext" with length 32. This beats "dir/subdir1/file1.ext" which has length 20.
Example 2 — Single File
$ Input: input = "file1.txt"
Output: 9
💡 Note: Only one file exists at root level with name "file1.txt", so the length is 9.
Example 3 — No Files
$ Input: input = "dir1\n\tdir2\n\t\tdir3"
Output: 0
💡 Note: All entries are directories (no extensions), so no files exist. Return 0.

Constraints

  • 1 ≤ input.length ≤ 104
  • input contains only valid directory/file names
  • File names contain exactly one '.' character
  • No empty directory or file names

Visualization

Tap to expand
Longest Absolute File Path INPUT File System Structure: dir subdir1 file1.ext subsubdir1 subdir2 subsubdir2 file2.ext Raw Input String: "dir\n \tsubdir1\n \t\tfile1.ext\n \t\tsubsubdir1\n \tsubdir2\n \t\tsubsubdir2\n \t\t\tfile2.ext" \t = tab (depth level) ALGORITHM STEPS 1 Split by newline Separate each line 2 Count tabs = depth Tabs indicate nesting level 3 Track path lengths Use stack/map for each depth 4 Check for file (has ".") Update max if file found Path Length at Each Depth: Depth Name Length 0 dir 3 1 subdir2 3+1+7=11 2 subsubdir2 11+1+10=22 3 file2.ext 22+1+9=32 +1 for "/" separator File found! Update max FINAL RESULT Longest Absolute Path: dir / subdir2 / subsubdir2 / file2.ext 3 + 1 + 7 + 1 + 10 + 1 + 9 = 32 Output: 32 Path string: "dir/subdir2/subsubdir2/ file2.ext" OK - File Found Key Insight: Use a hashmap/stack to track cumulative path length at each depth level. When processing a new entry, look up parent's length (depth-1), add current name length + 1 (for "/"). If it contains "." (is a file), update maximum. Time: O(n), Space: O(n) where n is input string length. TutorialsPoint - Longest Absolute File Path | Optimal Solution (Stack/HashMap)
Asked in
Google 35 Amazon 28 Microsoft 22 Facebook 18
67.0K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen