Problem, as an interviewer would state it:
"Build a simple client-side router: given a list of { pattern, handler } routes (supporting dynamic segments like :id and a wildcard *), match a real path against them and extract any named parameters."
Examples:
router.match("/users/42"); // { handler: "userDetail", params: { id: "42" } }
router.match("/users/42/posts/7"); // { handler: "userPost", params: { id: "42", postId: "7" } }
Clarifying questions expected:
- What should a route with MULTIPLE dynamic segments (like
/users/:id/posts/:postId) look like once matched? - Should routes be checked in ARRAY ORDER, with the first match winning, or should more "specific" routes always take precedence regardless of order?
- Does a wildcard
*segment need to capture the matched remainder as a param too, or just match anything?
Code / implementation expected: Yes — real, direct proof matching a single dynamic segment, multiple dynamic segments, a static route, a wildcard, and a genuinely unmatched path.