Longest Common Parent Directory
Updated
Problem statement
You are given one or more normalized absolute Unix-style file paths. Every path begins with /, contains no empty component and no . or .. component, and its final component is a file name.
Return the deepest directory that is a parent of every file path. Compare complete path components rather than raw character prefixes. Return / when the paths share no directory below the root.
Function
longestCommonParentDirectory(paths: String[]) → StringExamples
Example 1
paths = ["/home/user/docs/a.txt","/home/user/docs/b.txt","/home/user/photos/c.jpg"]return = "/home/user"All three files are beneath /home/user, but they do not all share the next directory.
Example 2
paths = ["/a/b/file.txt","/a/b/file.txt"]return = "/a/b"Duplicate file paths still have /a/b as their parent directory.
Example 3
paths = ["/a/x.txt","/b/y.txt"]return = "/"The files share only the root directory.
Constraints
1 <= paths.length <= 1000002 <= paths[i].length() <= 100000- The total number of characters across all paths is at most
1000000. - Each path is a normalized absolute path to a file.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Keyboard Autocomplete SuggestionsLyft · ONSITE INTERVIEWLyft interview questions
- Word Break IIAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Word Break IIBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Longest Word Containing Another InputMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Word Search IIKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Find All Word Occurrences in TextSentry · PHONE SCREENSentry interview questions
- Find the Words for Each PrefixDocuSign · PHONE SCREENDocuSign interview questions
- Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir interview questions
- Bounded Word Guess SolverAtlassian · ONSITE INTERVIEWAtlassian interview questions
- Filter Pins by Excluded IDsPinterest · ONSITE INTERVIEWPinterest interview questions