Longest Prefix Match Route Lookup With Prefix Removal
Last updated: November 29, 2026
Quick Overview
Build an IPv4 routing table that supports adding a prefix with a next hop, removing a prefix, and looking up the next hop for an address using longest prefix match.
Arista Networks
November 29, 20260
0
0 solved
Build an IPv4 routing table that supports adding a prefix with a next hop, removing a prefix, and looking up the next hop for an address using longest prefix match.
Route lookup is the core of every forwarding decision an EOS switch makes, and the routing agent must keep its RIB consistent as BGP and static routes come and go. The interviewer checks whether you reach for a binary trie instead of a linear scan, handle the default route, and clean up after deletes.
What the Interviewer Expects
- Choose a binary trie keyed on address bits, one level per prefix bit
- Return the deepest matching next hop, including a 0.0.0.0/0 default
- Support removal and prune empty branches so memory does not leak
- State O(32) lookup and insert cost independent of table size
- Parse dotted quads into 32-bit integers correctly
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you compress the trie (path compression, multibit strides) for a million-route table?
- How does hardware do this with TCAM, and why does TCAM need entries sorted by prefix length?
- What changes to support IPv6 128-bit prefixes?
- How would you let a reader do lookups while a writer updates routes?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Operations: add(prefix, next_hop), remove(prefix) returning whether it existed, lookup(ip) returning the next hop of the most specific covering prefix or None. Adding an existing prefix overwr...
High-Level Design
A sorted list or hash per prefix length works (check /32 down to /0, up to 33 hash probes), but a binary trie is the clearest: walk one bit per level from the most significant bit. Each node may c...