Longest prefix match routing
Implement a longest-prefix-match routing table lookup. Given a list of [cidr, next_hop] routes where cidr is an IPv4 prefix like '10.0.0.0/8', and a destination IPv4 address, return the next_hop of the most specific (longest prefix length) matching route. A route matches if (dest & mask) == (network & mask). If no route matches, return 'none'. A '/0' default route matches everything. All addresses are valid IPv4.
Implement
route_lookup(routes: list[list[str]], ip: str) → strExamples
in
[[["0.0.0.0/0","default"],["10.0.0.0/8","r1"],["10.1.0.0/16","r2"]],"10.1.2.3"]out"r2"What a strong answer looks like
State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
0:00 of about 22 min
solution.py
InputExpectedGot
[[["0.0.0.0/0","default"],["10.0.0.0/8","r1"],["10.1.0.0/16","r2"]],"10.1.2.3"]"r2"not run yetsample