Match File Paths with Wildcards
Updated
Problem statement
You are given an array of unique canonical absolute file paths and an absolute wildcard pattern. Return every file path that matches the entire pattern.
Pattern rules
- A slash separates path segments.
- Literal characters match case-sensitively.
- Inside one segment,
*matches any sequence of zero or more non-slash characters. - A run of stars inside a segment behaves as one
*unless the complete segment is exactly**. - A complete pattern segment equal to
**matches zero or more complete path segments.
Return each matching input path once, sorted in lexicographic order. A partial prefix or suffix match is not sufficient.
Function
matchFilePaths(filePaths: String[], pattern: String) → String[]Examples
Example 1
filePaths = ["/home/a.txt","/home/user/docs/a.txt","/home/user/docs/b.txt","/var/log/a.txt"]pattern = "/home/**/a.txt"return = ["/home/a.txt","/home/user/docs/a.txt"]The ** segment first matches zero directory segments and then matches user/docs. Paths outside /home and files with another final name do not match.
Example 2
filePaths = ["/home/user/docs/a.txt","/home/user/images/a.png","/home/admin/docs/a.txt","/var/log/app.log"]pattern = "/home/*/docs/a.txt"return = ["/home/admin/docs/a.txt","/home/user/docs/a.txt"]The single-star segment matches exactly one directory level. Both matching paths are returned in lexicographic order even though the input order is different.
Example 3
filePaths = ["/a/file.txt","/a/fe.txt","/a/fee.txt","/a/folder/file.txt"]pattern = "/a/f**e.txt"return = ["/a/fe.txt","/a/fee.txt","/a/file.txt"]Because the repeated stars occur inside a larger segment, they act as one segment-local wildcard. That wildcard may match an empty string, but it never crosses a slash.
Constraints
1 <= filePaths.length <= 50001 <= pattern.length <= 200- Every path and the pattern begin with
/, have no trailing slash, and contain no empty,., or..segment. - File-path segments contain letters, digits, dots, underscores, and hyphens. Pattern segments may additionally contain
*. - The total number of characters across
filePathsis at most200000.
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
- Encoded String MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Is Regex MatchingAmazon · OA
- Rank Word-Translation Hints from Sentence TranslationsDuolingo · PHONE SCREEN · ONSITE INTERVIEWDuolingo interview questions
- Response Time Stats by URL PatternGusto · ONSITE INTERVIEWGusto interview questions
- Invoice / Payment ReconciliationStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Most Frequent Call Path From Function Trace LogsRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Single-Pass S-Expression ParserBaseten · ONSITE INTERVIEWBaseten interview questions
- Maximum Bouquet ValueOracle · ONSITE INTERVIEWOracle interview questions
- Flatten a Mixed JSON ListNike, Inc. · ONSITE INTERVIEWNike, Inc. interview questions
- Transform Dotted Keys into a Nested MapProofpoint · ONSITE INTERVIEW