HardPro challengePythonJavaScriptTypeScript

HTTP Router (Trie)

Node.jsRoutingTrees

Build a trie-based router. solve(routes, queries) registers each route then
runs each query, returning an array of { handler, params } | null.

Routes are ['GET /users/:id', 'POST /posts/:slug/comments'] (method + path).
Queries are 'GET /users/42'. handler is the original route string.

Sample tests

Test #1Single param
Input: [["GET /users/:id"],["GET /users/42"]]
Output: [{"params":{"id":"42"},"handler":"GET /users/:id"}]
Test #2Two routes
Input: [["GET /a","GET /a/:x"],["GET /a","GET /a/y"]]
Output: [{"params":{},"handler":"GET /a"},{"params":{"x":"y"},"handler":"GET /a/:x"}]
Test #3Method mismatch
Input: [["GET /x"],["POST /x"]]
Output: [null]
Test #4No match
Input: [["GET /a/b"],["GET /a/c"]]
Output: [null]
Test #5Nested param
Input: [["POST /posts/:slug/comments"],["POST /posts/hello/comments"]]
Output: [{"params":{"slug":"hello"},"handler":"POST /posts/:slug/comments"}]