Design an HTTP Router
🧭 What is an HTTP router? It is the part of a web framework that decides which piece of code handles a request. A request arrives as a method and a path, like
GET /users/42. The router compares the path with the patterns the app registered, such as/users/:id, picks the best one, and hands the handler the values it captured (id = 42).
Interviewers like this problem because it is small to state and has one sharp edge: priority. Two patterns can both match a path, and the router must pick the same winner no matter in which order the routes were added. Getting that right leads naturally to a trie with backtracking, which is a good test of whether you can turn a rule into a data structure.
Requirements
"Design a router for a web framework. It has
addRoute(method, path, handler)andcallRoute(method, path). Support path parameters and wildcards."
That leaves a lot open. What exactly is a wildcard? What happens when two routes match? The questions below pin it down.
Clarifying Questions
You: What can a route pattern contain?
Interviewer: Three kinds of segment. A literal like users matches that exact text. A parameter like :id matches any one segment and captures it. A * matches the rest of the path, one or more segments, and is only allowed at the end.
You: When several routes match the same request, which one wins?
Interviewer: The most specific one. Compare the routes segment by segment from the left. At the first position where they differ, a literal beats a parameter, and a parameter beats a wildcard. The order in which routes were added must not matter.
You: What does callRoute return?
Interviewer: The handler's name plus the captured values in path order, like getPost userId=7 postId=99. A wildcard's value is the rest of the path, captured under the name *.
You: What if nothing matches? Do we tell apart "no such path" and "wrong method"?
Interviewer: Yes. If the path matches a route of some other method, return 405. If no route matches at all, 404.
You: Are /users and /users/ the same path?
Interviewer: Yes. Ignore empty segments, so trailing and double slashes don't matter.
You: What if someone adds the same route twice?
Interviewer: Reject it. Two routes with the same method and the same shape are duplicates, even if their parameters have different names: /users/:id and /users/:name can never be told apart.
You: Will the router be called from many threads?
Interviewer: Yes, every request calls it. But routes are all added at startup. We can talk about changing them at runtime later.
The second answer is the one to write down. "Most specific wins, compared from the left" is a precise rule, and a precise rule can be turned into a precise search. "The first route that matches wins" would be simpler to build but would make the result depend on the order of addRoute calls, which is a classic source of bugs in real apps.
💡 Interview tip: When a problem says "best match", ask for the exact tie-breaking rule and repeat it back with an example: "So for
/users/me,/users/mebeats/users/:id, which beats/users/*?" Interviewers often have one specific rule in mind, and the tests will check it.
Final Requirements
addRoute(method, path, handler)registers a route. A route segment is a literal, a parameter:name, or a final*.- A route with the same method and shape as an existing one is rejected (
false). Parameter names don't count toward the shape. callRoute(method, path)returns the winning route's handler and its captured values in path order, asname=valuepairs.- A literal matches the same text, a parameter matches any one segment, and
*matches one or more remaining segments. - Among the matching routes of that method, the winner is decided at the first segment where they differ: literal beats parameter beats wildcard. Registration order never matters.
- If no route of the method matches, the answer is
405when a route of another method matches the path, and404otherwise. - Empty segments are ignored:
/users/,//usersand/usersare the same path, and/has no segments.
Out of scope
- Running the handlers, request bodies and responses.
- Query strings, URL decoding and host-based routing.
- Regular-expression constraints on parameters and middleware (both come back in Extensibility).