Route Packets by Longest Prefix Match in a Virtual Network
Last updated: November 29, 2026
Quick Overview
Given a route table of CIDR prefixes and next hops, like an Azure virtual network route table with a default route, peered ranges, and a firewall subnet, implement lookup that returns the next hop for the most specific matching prefix.
Microsoft
November 29, 20260
0
0 solved
Given a route table of CIDR prefixes and next hops, like an Azure virtual network route table with a default route, peered ranges, and a firewall subnet, implement lookup that returns the next hop for the most specific matching prefix.
Azure networking teams expect engineers to understand how routing decisions are made, and this is a clean way to test CIDR math and data structure choice together. The interviewer looks for correct bit handling, why the most specific route wins, and a structure faster than scanning every route.
What the Interviewer Expects
- Explains CIDR notation and what the prefix length means
- Implements longest-prefix match correctly, including the default route
- Uses a trie keyed on address bits instead of a linear scan
- Rejects prefixes with host bits set
- Discusses compressed tries or per-length hash tables for performance
Key Topics to Cover
How to Approach This
- Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
- Choose data structures based on access patterns, not familiarity.
- Prefer immutable data and message passing over shared mutable state for concurrency.
- Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
- How would you support IPv6 with the same design?
- How would you make lookups faster than 32 steps?
- How would route updates work while lookups are in flight?
- How would you resolve two routes with the same prefix from different sources?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice System Design ProblemsSample Answer
Requirements
add(cidr, next_hop)inserts a route such as10.1.0.0/16 -> peered-vnet.lookup(ip)returns the next hop of the longest prefix that contains the IP, orNoneif nothing matches.- `0.0.0...
High-Level Design
An IPv4 prefix a.b.c.d/n matches every address whose first n bits equal the prefix's first n bits. Several routes can match one address; the one with the largest n is most specific and wins, which l...