Route API Requests by Path Segments With Wildcard Precedence
Last updated: November 29, 2026
Quick Overview
Build a router for an API gateway that registers path patterns like /users/*/files and dispatches incoming paths to the matching handler, preferring literal segments over "*" and backtracking when a literal branch dead-ends.
Microsoft
November 29, 20260
0
0 solved
Build a router for an API gateway that registers path patterns like /users/*/files and dispatches incoming paths to the matching handler, preferring literal segments over "*" and backtracking when a literal branch dead-ends.
An onsite coding question in the style of Azure API Management and ASP.NET routing teams, where routing tables hold thousands of templates. The interviewer checks that you model segments, not characters, as trie edges and that precedence plus backtracking is handled correctly.
What the Interviewer Expects
- Split paths into segments and build a trie on segments
- Store the handler on the terminal node
- Try the literal child before the wildcard child
- Backtrack to the wildcard when the literal branch fails deeper down
- Discuss the worst-case cost of backtracking
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you capture the wildcard values as named parameters like {userId}?
- How would you add a trailing "**" that matches any remaining segments?
- How would you detect two registered routes that are ambiguous?
- How would you make lookups lock-free while routes are hot-reloaded?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Patterns and paths are slash-separated. A pattern segment is either a literal or *, which matches exactly one path segment. Matching must consume every segment. When several patterns match, the one ...
High-Level Design
A trie whose edges are whole segments. Registration walks or creates one node per segment and stores the handler under a None key on the last node. Lookup is a DFS that, at each depth, tries the exa...