Longest Absolute File Path

MediumStackhashing

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

A file system is serialized into a single string input. Each line (lines are separated by \n) holds one entry, and the number of leading tab characters \t on a line is that entry's depth. An entry at depth d + 1 lives inside the nearest entry above it at depth d. The top-level entries have depth 0. Only tab characters indicate depth; any spaces are part of the name. An entry is a **file** if its name contains a ., otherwise it is a directory. The absolute path of a file joins the names from the top-level directory down to the file with /, e.g. dir/subdir2/file.ext. Return the length of the longest absolute path to any file. If there are no files, return 0.

Examples

Input: input = "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext"
Output: 20
The only file is `dir/subdir2/file.ext`, which has 20 characters.
Input: input = "dir\n\tsubdir1\n\t\tfile1.ext\n\t\tsubsubdir1\n\tsubdir2\n\t\tsubsubdir2\n\t\t\tfile2.ext"
Output: 32
The files are `dir/subdir1/file1.ext` (21) and `dir/subdir2/subsubdir2/file2.ext` (32).
Input: input = "a"
Output: 0
There is a directory but no file.

Constraints

  • 1 <= input.length <= 10^4
  • `input` contains letters, digits, spaces, `.`, `\n` and `\t`
  • Every name is non-empty and the depth structure is valid
  • Directory names contain no `.`

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More hashing problems