Design a trie-based URL router with wildcards
AtlassianSoftware Engineer · Technical ScreenLast updated
Design a trie-based URL router with wildcards evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Question
Implement a URL routing matcher that supports adding route patterns and matching request paths, using a trie (prefix tree) as the core data structure. Paths are split into /-delimited segments. Support static segments (e.g., /a/b/c) and a single-segment wildcard * that matches exactly one segment (e.g., /a/*/c).
Design and implement the following:
- APIs. Provide
addRoute(pattern, handler),removeRoute(pattern), andmatch(path) -> (matchedHandler, matchedPattern). Routes must be addable and removable at runtime. - Wildcard matching.
*matches exactly one path segment. For example,/a/*/cmatches/a/x/cand/a/y/cbut not/a/cor/a/x/y/c. - Precedence rules. Define and implement deterministic precedence when multiple registered patterns could match a path — for example, prefer a more specific (static) match over a wildcard match.
- Complexity. Analyze the time and space complexity of insertion, removal, and lookup.
- Concurrency. Explain strategies for making the structure thread-safe when reads and writes (route add/remove) happen concurrently.
- Tests. Provide unit tests covering edge cases such as leading/trailing slashes, the root path
/, duplicate routes, and overlapping wildcard patterns.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.