Build A Longest Prefix Match Routing Table With Deletes

Last updated: November 29, 2026

Quick Overview

Implement a routing table that supports insert(prefix, next_hop), delete(prefix), and lookup(ip) returning the next hop of the most specific matching prefix, including a default route.

Cisco
Coding & Algorithms
Software Engineer
Cisco
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Implement a routing table that supports insert(prefix, next_hop), delete(prefix), and lookup(ip) returning the next hop of the most specific matching prefix, including a default route.

Every Cisco router, from ISR branch boxes to Silicon One based 8000 series, resolves destinations with longest prefix match, and software RIB and FIB code is core to IOS XR and NX-OS teams. The interviewer wants a binary trie with correct delete pruning and a discussion of how real FIBs compress it.

What the Interviewer Expects
  • Uses a binary trie indexed by address bits
  • Stores next hop at the node for the prefix length
  • Tracks the best match while descending, including the root for 0.0.0.0/0
  • Deletes the route and prunes empty branches
  • Discusses compressed tries or TCAM for production
Key Topics to Cover
longest prefix match
binary trie
routing tables
IPv4
tree pruning
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 a Patricia or multibit trie reduce memory and lookup steps?
  • How does a TCAM do this in one lookup, and what is the cost?
  • How would you support ECMP, multiple next hops per prefix?
  • How would you let readers do lookups while a BGP update thread writes?
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

Prefixes are IPv4 CIDR strings. Insert on an existing prefix replaces the next hop (route update). Delete returns whether the route existed. Lookup returns the next hop of the longest matching prefix,...

High-Level Design

A binary trie: depth i corresponds to bit i of the address, most significant first. A prefix of length L lives at depth L. Lookup walks up to 32 levels, remembering the last node with a next hop. Dele...


Submit Your Answer
Markdown supported

Related Questions