Longest Prefix Match for IPv4 Routes in an Edge Router Table

Last updated: November 29, 2026

Quick Overview

Given a routing table of IPv4 CIDR prefixes such as 10.1.0.0/16 mapped to next hops, implement lookup(ip) that returns the next hop of the most specific (longest) matching prefix, or None if nothing matches.

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

0

0

0 solved


Given a routing table of IPv4 CIDR prefixes such as 10.1.0.0/16 mapped to next hops, implement lookup(ip) that returns the next hop of the most specific (longest) matching prefix, or None if nothing matches.

Meta runs its own edge network and load balancers, so infra-leaning interviewers like a coding question grounded in routing. The interviewer is checking IPv4 parsing into a 32-bit integer, a binary trie keyed by prefix bits, and remembering the best match seen during the walk.

What the Interviewer Expects
  • Parse dotted quads and prefix lengths into integers
  • Insert only the first prefix-length bits of each route
  • Track the most recent next hop while walking the address bits
  • Handle a /0 default route
  • Discuss compressed tries for production
Key Topics to Cover
binary trie
string parsing
bit manipulation
longest prefix match
networking
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 support route withdrawal?
  • How do Patricia or multibit tries reduce memory and lookups?
  • How would you extend this to IPv6?
  • How would you validate malformed input?
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
  • Routes: list of (cidr, hop) like ('10.1.0.0/16', 'edge-a'). Lengths range from 0 to 32.
  • lookup('10.1.2.3') returns the hop of the longest matching prefix.
  • If two routes have the same prefi...
High-Level Design

First, parse: split a.b.c.d on dots and combine as (a<<24)|(b<<16)|(c<<8)|d. For a route, split off /len.

Build a binary trie. To insert a route, walk the top len bits of its address (bit 31 ...


Submit Your Answer
Markdown supported