Design a trie-based URL router with wildcards
Company: Atlassian
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
##### 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:
1. **APIs.** Provide `addRoute(pattern, handler)`, `removeRoute(pattern)`, and `match(path) -> (matchedHandler, matchedPattern)`. Routes must be addable and removable at runtime.
2. **Wildcard matching.** `*` matches exactly one path segment. For example, `/a/*/c` matches `/a/x/c` and `/a/y/c` but not `/a/c` or `/a/x/y/c`.
3. **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.
4. **Complexity.** Analyze the time and space complexity of insertion, removal, and lookup.
5. **Concurrency.** Explain strategies for making the structure thread-safe when reads and writes (route add/remove) happen concurrently.
6. **Tests.** Provide unit tests covering edge cases such as leading/trailing slashes, the root path `/`, duplicate routes, and overlapping wildcard patterns.
Quick Answer: 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.
Solution
# Solution Alignment
The prompt asks for an implementation-level answer. The safest way to present it is to define the state, maintain clear invariants, then walk through complexity and tests.
## Problem Restatement
##### 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: 1. **APIs.** Provide `addRoute(pattern, handler)`, `removeRoute(pattern)`, and `match(path) -> (matchedHandler, matchedPattern)`. Routes must be addable and removable at runtime. 2. **Wildcard matching.** `*` matches exactly one path segment. For example, `/a/*/c` matches `/a/x/c` and `/a/y/c` but not `/a/c` or `/a/x/y/c`. 3....
## Recommended Approach
Choose traversal based on the required view or aggregate. DFS is natural for subtree computations and reconstruction; BFS is natural for level order or side views. Keep per-depth or per-position state when the output depends on columns, rows, or depths.
## Correctness
The implementation should maintain an invariant after each loop or operation that directly matches the problem statement. At termination, that invariant implies the returned value has considered every valid candidate exactly once, or has preserved the required data-structure state after every API call.
## Complexity
Most tree traversals are O(n) time and O(h) recursion stack for DFS or O(w) queue space for BFS, where h is height and w is maximum width.
## Edge Cases and Tests
Empty tree, one node, skewed tree, duplicate values when reconstruction assumes uniqueness, deep recursion, and tie-breaking for same row/column nodes.
Explanation
Rubric: the candidate should (1) choose a segment-keyed trie with a dedicated wildcard edge per node (not a character trie); (2) implement addRoute/removeRoute/match cleanly, including path normalization for leading/trailing slashes and the root path; (3) get precedence right — static beats wildcard via a DFS that tries the exact branch first and BACKTRACKS to the wildcard, since a greedy exact-first walk fails on inputs like /a/x/c with patterns {/a/x/d, /a/*/c}; (4) enforce single-segment wildcard semantics (matches exactly one segment); (5) analyze complexity (O(S) add/remove, O(S) match without wildcards, with the wildcard-backtracking caveat) and prune empty nodes on removal; and (6) discuss thread safety, ideally landing on copy-on-write with an atomic root swap for lock-free reads in a read-heavy router, or at minimum a read-write lock. Strong answers also state a duplicate-route policy and write tests for the listed edge cases.