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
Coding & Algorithms
Software Engineer
Arista Networks
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

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
binary trie
longest prefix match
bit manipulation
IPv4
routing table
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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...


Submit Your Answer
Markdown supported

Related Questions