← All Exercises
Rank Route Patterns by Specificity
intermediatejavascript
Background
API gateways and routers resolve overlapping patterns with a specificity rule. Getting the ordering wrong makes /users/me hit /users/:id and silently returns the wrong resource.
Problem
Given a set of registered route patterns and one incoming request path, return the pattern that should handle it: static segments beat parameter segments, which beat wildcard segments.
Input
JSON object: { paths: string[], requestPath: string }Output
JSON string: the winning pattern, or null when nothing matches
Sample tests
Sample 1
Input: {"paths":["/users/me","/users/:id","/files/*"],"requestPath":"/users/me"}Expected: "/users/me"
Sample 2
Input: {"paths":["/users/me","/users/:id","/files/*"],"requestPath":"/users/42"}Expected: "/users/:id"
Sample 3
Input: {"paths":["/files/*","/users/:id"],"requestPath":"/files/a/b"}Expected: "/files/*"
Your solution
Learning objective
Resolve overlapping route patterns deterministically instead of relying on registration order.