Longest Prefix Match Lookup for a Cloud Routing Table

Last updated: November 29, 2026

Quick Overview

Given CIDR routes such as 10.0.0.0/8 with next hops, answer lookups for IPv4 addresses by returning the next hop of the most specific matching route, or None.

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

0

0

0 solved


Given CIDR routes such as 10.0.0.0/8 with next hops, answer lookups for IPv4 addresses by returning the next hop of the most specific matching route, or None.

A Google onsite coding question with a Google Cloud networking flavor, close to what a VPC router does for every packet. The interviewer checks a binary trie over prefix bits, tracking the deepest route seen during the walk, and parsing details.

What the Interviewer Expects
  • Parse dotted IPv4 and CIDR lengths into integers
  • Insert each route's first L bits into a binary trie
  • Walk an address's bits and remember the last route node passed
  • Return the default route 0.0.0.0/0 when present
  • Discuss path compression and multibit tries for speed
Key Topics to Cover
binary trie
longest prefix match
CIDR
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 would a multibit (stride 8) trie change memory and lookup time?
  • How would you handle IPv6?
  • How do hardware routers do this at line rate?
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

add(cidr, hop) stores a route like 192.168.1.0/24. lookup(ip) returns the hop of the matching route with the largest prefix length, or None. Bits beyond the prefix length in a CIDR are ignored....

High-Level Design

A route of length L matches an address iff the address's top L bits equal the route's top L bits. Insert each route as a path of L bits from the most significant bit in a binary trie, storing ...


Submit Your Answer
Markdown supported