Match Request Paths Against Next.js File-System Route Patterns
Last updated: November 29, 2026
Quick Overview
Given a set of route patterns using static segments, dynamic segments ([slug]), catch-all segments ([...path]) and optional catch-all segments ([[...path]]), return the most specific route matching a request path along with its extracted params.
Vercel
November 29, 20260
0
0 solved
Given a set of route patterns using static segments, dynamic segments ([slug]), catch-all segments ([...path]) and optional catch-all segments ([[...path]]), return the most specific route matching a request path along with its extracted params.
Next.js maps files in the app directory to URLs, and the router must always pick the most specific match so /blog/new beats /blog/[slug]. This screen checks whether a candidate can define a precise specificity order, parse segments cleanly and handle the subtle difference between required and optional catch-alls.
What the Interviewer Expects
- Define specificity: static before dynamic before catch-all before optional catch-all, compared segment by segment
- Return extracted params with catch-alls as lists
- Treat a required catch-all as needing at least one segment and an optional one as allowing zero
- Reject or flag catch-alls that are not the last segment
- Discuss precomputing the order once at build time rather than per request
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you build a trie so matching is proportional to path length instead of route count?
- Two patterns like /[a]/x and /x/[b] both match /x/x. Which should win, and should the build fail?
- How would you add route groups like (marketing) that do not appear in the URL?
- How would you support percent-encoded segments safely?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Patterns are slash-separated. Segment kinds: static about, dynamic [slug] (exactly one segment), catch-all [...path] (one or more remaining segments, must be last), optional catch-all `[[...path...
High-Level Design
Give each segment a rank: static 0, dynamic 1, catch-all 2, optional catch-all 3. A pattern's sort key is its list of ranks, compared lexicographically, so the first segment where two patterns differ ...