←ENG-DSA-018Dynamic API Routing (Trie)
00:00
🐍 Python Idle
Mode:Web

Dynamic API Routing (Trie)

CompaniesVercel, Next.js
Est. Time~50 min
LevelSenior
TrieDesign
🚨 P0 Incident
VercelNext.js

Next.js's file-based routing system must match incoming request paths like "/users/123/profile" against registered routes like "/users/*/profile". A HashMap works for exact matches, but wildcard routing requires a Trie. This is the same algorithm used by Express.js, FastAPI, and virtually every web framework's router.

πŸ“₯ Assigned to:You β€” Senior Engineer
Your Task

You are writing the core router for a new web framework. You need to support fast path matching, including wildcard parameters (e.g., `/users/*/profile`). Implement an `add_route(path)` and `match_route(path)` using a Trie data structure.

⏱ Time: O(L) per operation where L=path lengthπŸ’Ύ Space: O(N*L)
Example 1
Input:Β Β add("/api/users"), match("/api/users")
Output:Β True
Example 2
Input:Β Β add("/api/users/*"), match("/api/users/123")
Output:Β True
πŸ”’ +3 hidden test cases β€” revealed on Submit
Hints
Loading editor…
Test Output
β–Ά Run Code to test against examples Β· Submit to judge all 5 test cases
EngPrep β€” Real Engineering. Real Interviews.