Match file paths stored in a trie against a glob pattern with ?, * and ** wildcards

Quick Overview

Store a list of file paths in a trie and return every path that matches a glob pattern with ?, * and double-star wildcards, in sorted order. Tests trie construction, wildcard matching inside a path segment, and a memoized search when a double star can absorb any number of directories.

Match file paths stored in a trie against a glob pattern with ?, * and ** wildcards

Company: Uber

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

Build a trie data structure to store file paths and implement a function to find all paths matching a glob-style wildcard pattern. You are given a list of file paths `paths` and a search `pattern`. The pattern may contain the following wildcards: - `?` matches exactly one character (excluding `/`) - `*` matches zero or more characters (excluding `/`) - `**` matches zero or more whole path segments (including `/`) Return all stored file paths that match the given pattern, sorted in lexicographical order. ### Function Signature ```python def search_paths(paths: list[str], pattern: str) -> list[str]: ``` ### Rules - Paths and the pattern are split on `/` into segments. Every path starts with `/`, so its first segment is the empty string. - `?` and `*` work inside a single segment and never match `/`. Every other pattern character matches itself. - A pattern segment that is exactly `**` matches zero or more whole path segments. It never matches part of a segment: `/a/**/d.txt` matches `/a/d.txt` and `/a/b/c/d.txt` but not `/a/bd.txt`. With zero segments the slash next to it is absorbed too, so `/a/**` also matches `/a`. - The whole path must match the whole pattern. A pattern that only matches a prefix of a path does not count. - Each matching path appears once in the output, even if it occurs several times in `paths`. Sort the output ascending by plain character-code comparison (the order Python's `sorted` gives). Return an empty list when nothing matches. ### Constraints - `1 <= len(paths) <= 1000` - `1 <= len(paths[i]) <= 200` - All paths start with `/` - Paths contain only lowercase letters, digits, `/`, `.`, `-`, and `_` - `1 <= len(pattern) <= 200` - The pattern contains only lowercase letters, digits, `/`, `.`, `-`, `_`, `?`, and `*` - Two `*` characters are adjacent only in a pattern segment that is exactly `**` ### Examples ```text Input: paths = ["/home/user/docs/file.txt", "/home/user/pics/photo.jpg", "/home/admin/docs/report.pdf", "/home/user/docs/notes.md", "/var/log/syslog"], pattern = "/home/*/docs/*.txt" Output: ["/home/user/docs/file.txt"] ``` The first `*` matches any single segment, so both `user` and `admin` pass it. In the two `docs` folders, only `file.txt` ends with `.txt`. ```text Input: paths = ["/a/b/c/d.txt", "/a/x/d.txt", "/a/d.txt", "/a/b/d.txt"], pattern = "/a/**/d.txt" Output: ["/a/b/c/d.txt", "/a/b/d.txt", "/a/d.txt", "/a/x/d.txt"] ``` The `**` matches zero segments for `/a/d.txt`, one segment for `/a/b/d.txt` and `/a/x/d.txt`, and two segments for `/a/b/c/d.txt`, so all four paths match. ```text Input: paths = ["/src/a1.py", "/src/ab.py", "/src/lib/a2.py", "/src/a.py", "/src/xa1.py"], pattern = "/src/**/a?.py" Output: ["/src/a1.py", "/src/ab.py", "/src/lib/a2.py"] ``` The last segment must be `a`, one character, then `.py`. `a.py` is one character short. `xa1.py` fails because `**` can only absorb whole segments, so it cannot take the leading `x` on its own.

Overview: Store a list of file paths in a trie and return every path that matches a glob pattern with ?, * and double-star wildcards, in sorted order. Tests trie construction, wildcard matching inside a path segment, and a memoized search when a double star can absorb any number of directories.

Uber logo
Uber
Nov 25, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Build a trie data structure to store file paths and implement a function to find all paths matching a glob-style wildcard pattern.

You are given a list of file paths paths and a search pattern. The pattern may contain the following wildcards:

  • ? matches exactly one character (excluding / )
  • * matches zero or more characters (excluding / )
  • ** matches zero or more whole path segments (including / )

Return all stored file paths that match the given pattern, sorted in lexicographical order.

Function Signature

def search_paths(paths: list[str], pattern: str) -> list[str]:

Rules

  • Paths and the pattern are split on / into segments. Every path starts with / , so its first segment is the empty string.
  • ? and * work inside a single segment and never match / . Every other pattern character matches itself.
  • A pattern segment that is exactly ** matches zero or more whole path segments. It never matches part of a segment: /a/**/d.txt matches /a/d.txt and /a/b/c/d.txt but not /a/bd.txt . With zero segments the slash next to it is absorbed too, so /a/** also matches /a .
  • The whole path must match the whole pattern. A pattern that only matches a prefix of a path does not count.
  • Each matching path appears once in the output, even if it occurs several times in paths . Sort the output ascending by plain character-code comparison (the order Python's sorted gives). Return an empty list when nothing matches.

Constraints

  • 1 <= len(paths) <= 1000
  • 1 <= len(paths[i]) <= 200
  • All paths start with /
  • Paths contain only lowercase letters, digits, / , . , - , and _
  • 1 <= len(pattern) <= 200
  • The pattern contains only lowercase letters, digits, / , . , - , _ , ? , and *
  • Two * characters are adjacent only in a pattern segment that is exactly **

Examples

Input: paths = ["/home/user/docs/file.txt", "/home/user/pics/photo.jpg", "/home/admin/docs/report.pdf", "/home/user/docs/notes.md", "/var/log/syslog"], pattern = "/home/*/docs/*.txt"
Output: ["/home/user/docs/file.txt"]

The first * matches any single segment, so both user and admin pass it. In the two docs folders, only file.txt ends with .txt.

Input: paths = ["/a/b/c/d.txt", "/a/x/d.txt", "/a/d.txt", "/a/b/d.txt"], pattern = "/a/**/d.txt"
Output: ["/a/b/c/d.txt", "/a/b/d.txt", "/a/d.txt", "/a/x/d.txt"]

The ** matches zero segments for /a/d.txt, one segment for /a/b/d.txt and /a/x/d.txt, and two segments for /a/b/c/d.txt, so all four paths match.

Input: paths = ["/src/a1.py", "/src/ab.py", "/src/lib/a2.py", "/src/a.py", "/src/xa1.py"], pattern = "/src/**/a?.py"
Output: ["/src/a1.py", "/src/ab.py", "/src/lib/a2.py"]

The last segment must be a, one character, then .py. a.py is one character short. xa1.py fails because ** can only absorb whole segments, so it cannot take the leading x on its own.