How does a router choose between several matching routes? Explain longest prefix match.
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
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 DWhy 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.