Python exercise · about 5 minutes · free
API Gateway: Longest-Prefix Routing
One public entry point forwards /api/users to the user service and everything else under /api to the core service.
An API gateway is the single front door: it routes requests to services and can handle authentication, rate limiting, TLS, caching and logging in one place. Routing usually picks the longest matching prefix on a segment boundary. Pitfalls: turning the gateway into a business-logic monolith, and making it a single point of failure without redundancy.
Prices, versions and service names last checked 2026-11-19. They change, so confirm on the official pages before you rely on them.
Your quest: Write
route(routes, path). routes maps a prefix to a service name. Find the longest prefix that equals the path or is followed by / in the path (so /api/users does not match /api/usersX). Return (service, remaining_path), where the remainder keeps its leading slash ("/" if empty), or None when nothing matches.Try it now Runs in your browser. No sign-in needed. Ctrl+Enter to run.
Hint
A prefix matches if <code>path == prefix or path.startswith(prefix + '/')</code>. Choose the longest, then slice it off.Output appears here.