DescriptionQ266
Q266FWArchASIC interview problem
Longest-Prefix-Match Routing Table
TechniquesFWArchDVTrieIPv4Prefix matching
DifficultyHard
TopicData Structures
LanguageSystemVerilog
Requirements4 checkpoints
01
Problem
Implement an IPv4 routing table that returns the next hop associated with the longest prefix matching a 32-bit destination address.
Class declarationSystemVerilog
class RouteTable;
bit insert(bit [31:0] prefix, int prefix_len, int next_hop);
bit remove(bit [31:0] prefix, int prefix_len);
bit lookup(bit [31:0] addr, output int next_hop);
endclassExample input and output
Use this case to check your interpretationInput
routes: 10.0.0.0/8 -> A, 10.1.0.0/16 -> B
lookup: 10.1.2.3Output
next_hop = BExplanation
Both prefixes match, but /16 is longer and therefore wins over the broader /8 route.
02
Requirements (4)
- Support prefix lengths from /0 through /32.
- A repeated insert updates the existing route rather than creating a duplicate.
- Lookup must select the most-specific match and report failure if none exists.
- Normalize or reject prefixes whose host bits are nonzero.
