Skip to interview prep
Apexed
Problem list116 / 143

116. Resolve an API Route

MediumAPI routing

A route pattern consists of slash-delimited segments. A segment starting with a colon matches any one nonempty path segment. All other segments require exact, case-sensitive equality.

A route must match the entire path. Return the index of the matching pattern with the most literal segments, breaking ties by earlier input order. Return -1 when no pattern matches.

Example 1

Input: patterns = ["/tasks/:id","/:group/:id","/tasks/pinned"], path = "/tasks/pinned"
Output: 2

Example 2

Input: patterns = ["/:group/active","/jobs/:status"], path = "/jobs/active"
Output: 0

Constraints

  • At most 1,000 patterns, each with at most 20 segments.
  • Patterns and paths begin with a slash. Only the root path has a trailing slash. There are no empty segments, query strings or fragments.

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 6 hidden and sample tests.

Code runs in an isolated browser sandbox. Progress and submissions are saved on this device.

Resolve an API Route — coding interview practice — Apexed