Interview Q&A / Computer Networks

IntermediateQuick answer1 min readUpdated 3 Oct 2026

How does a router choose between several matching routes? Explain longest prefix match.

SHORT ANSWER

The router picks, among all table entries that contain the destination address, the one with the longest prefix (most specific). A /24 beats a /16, which beats a /8, and 0.0.0.0/0 is the last-resort default route.

Explain it simply

Asking for a house in a city: a directory entry for "Flat 4B, Rose Apartments, MG Road" is more useful than "somewhere in Pune". The more specific entry wins.

Example

text
Routing table
10.0.0.0/8      -> next hop A
10.1.0.0/16     -> next hop B
10.1.2.0/24     -> next hop C
0.0.0.0/0       -> next hop D   (default)

Destination      Matches                       Chosen
10.1.2.9         /8, /16, /24, /0              C   (/24 is longest)
10.1.9.9         /8, /16, /0                   B
10.200.1.1       /8, /0                        A
8.8.8.8          /0 only                       D

Why is 10.1.9.9 not in 10.1.2.0/24? The /24 covers 10.1.2.0 to 10.1.2.255 only, and 10.1.9.9 has third octet 9.

This rule lets a network advertise a big block and still carve out exceptions. It is also how a more specific route lets one customer be routed differently from the rest of their provider's block.

What to say in the interview

"Routers forward using the most specific matching entry, meaning the longest prefix. If nothing else matches, the default route 0.0.0.0/0 is used. Administrative distance and metric only matter when prefix lengths tie."

Common mistakes

  • Picking the first matching line rather than the longest.

#routing#longest-prefix-match#forwarding

Related questions

Practise more Computer Networks questions

Test your recall with short answers, then return to the detailed explanations when you need context.

Open Computer Networks quick answers →

Get new questions like this by email

One short email when something new and useful lands. No spam, unsubscribe in one tap.

By subscribing you agree to the Privacy Policy.