IP routing table with longest prefix match
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked at Snowflake. It tests trie, bit manipulation and networking.
Build the forwarding table of an IPv4 router. Routes are CIDR prefixes mapped to a next-hop name. When a packet arrives, the router must send it to the next hop of the most specific route that covers its destination address (longest prefix match).
Implement a class RoutingTable:
-
add_route(cidr: str, next_hop: str) -> Noneadds a route such as"10.1.0.0/16". If the same prefix already exists, replace its next hop. Bits after the prefix length are ignored, so"10.1.2.77/24"is the same prefix as"10.1.2.0/24". -
remove_route(cidr: str) -> boolremoves a route. ReturnTrueif it existed, otherwiseFalse. Removing a prefix must not affect longer prefixes inside it. -
lookup(ip: str) -> str | Nonereturns the next hop of the longest prefix that containsip, orNoneif no route matches. A/0route (0.0.0.0/0) is the default route and matches every address.
Constraints:
- Addresses are dotted-quad IPv4 strings. Prefix lengths are 0 to 32. Raise
ValueErrorfor malformed input. - Up to 10^5 routes and 10^6 lookups. A lookup must not scan every route.
Example:
add_route("172.16.0.0/12", "core")
add_route("172.20.0.0/16", "east")
add_route("172.20.5.0/24", "rack-5")
lookup("172.20.5.40") -> "rack-5"
lookup("172.20.9.1") -> "east"
lookup("172.31.0.1") -> "core"
lookup("8.8.8.8") -> None
add_route("0.0.0.0/0", "upstream")
lookup("8.8.8.8") -> "upstream"
remove_route("172.20.0.0/16") -> True
lookup("172.20.9.1") -> "core"
lookup("172.20.5.40") -> "rack-5"
Clarifying questions
- Should octets with leading zeros, such as
"010.1.1.1", be rejected or read as decimal? - Does removing a prefix that was never added raise an error, or return
False? - Are reads much more frequent than updates, and can updates happen during lookups?
- Do we need to list or count routes, or only add, remove and look up?
What a strong answer covers
- Parses the address into a 32-bit integer and masks the host bits, so
"10.1.2.77/24"and"10.1.2.0/24"are the same key. It raisesValueErrorfor a bad octet count, a value over 255, a non-number or a length over 32. - Stores routes in a binary trie keyed by the prefix bits, with the next hop on the node at the prefix's depth. A lookup walks at most 32 bits and remembers the last next hop it passed.
- Or keeps 33 hash maps, one per prefix length, and checks them from
/32down to/0. It compares the two designs: memory, the cost of a miss, and the number of probes per lookup. -
remove_routeclears only that node's next hop, and can prune empty branches, so longer prefixes below it are not affected. - Tests the default route,
/32host routes, replacing a next hop, removing a prefix that has longer routes inside it, and a lookup with no match.
Follow-ups
- IPv6, with 128-bit addresses.
- Make the trie smaller and faster with path compression (a Patricia trie) or multi-bit strides.
- Lookups and route changes from several threads: locks, read-copy-update, or swapping in a new table.
- Return every route that covers an address, most specific first.
Where it's asked
What it tests
trie · bit manipulation · networking · design
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
