FastPrepLongest Common Parent Directory
Problems/Longest Common Parent Directory

Longest Common Parent Directory

Google logoGoogle● EasyINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A Google intern virtual onsite report asks for the deepest common parent directory across paths. It discusses duplicate paths, paths of different depths, a trie option, and saving memory at very large scale. The report does not give a signature or path-cleaning rules. Here we use cleaned absolute file paths with no dot segments.

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[]) → String

Examples

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 <= 100000
  • 2 <= 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

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public String longestCommonParentDirectory(String[] paths) {
        // Write your code here.
    }

}
Checking account…