Code RoomCanonical routing table
MediumPrep Room Coding #4883

Canonical routing table

CodingNetworking & APIsAlgorithms & data structuresMid–Senior~25 min

A router walks its forwarding table one entry at a time and stops at the first entry whose prefix covers the destination, so the table has to be laid out most specific first. Each entry is written as an IPv4 address, a slash, and a prefix length from 0 to 32, for example 10.4.3.9/16. The four octets carry no leading zeros. The address may carry bits below the prefix length that do not belong to the network, so 10.4.3.9/16 names the same route as 10.4.0.0/16 and has to be printed in that canonical form. Return the canonical entries ordered by prefix length from long to short, and within one length by network address ascending as a number. When two entries reduce to the same canonical route, keep only the one given first. An empty table returns an empty list.

Implement
order_route_table(entries: list[str]) → list[str]
Examples
in[["10.4.3.9/16","10.4.3.9/24","0.0.0.0/0"]]out["10.4.3.0/24","10.4.0.0/16","0.0.0.0/0"]
in[["192.168.1.5/24","192.168.1.200/24"]]out["192.168.1.0/24"]
in[["10.2.0.0/16","10.1.0.0/16"]]out["10.1.0.0/16","10.2.0.0/16"]
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 25 min
InputExpectedGot
[["10.4.3.9/16","10.4.3.9/24","0.0.0.0/0"]]["10.4.3.0/24","10.4.0.0/16","0.0.0.0/0"]not run yetsample
[["192.168.1.5/24","192.168.1.200/24"]]["192.168.1.0/24"]not run yetsample
[["10.2.0.0/16","10.1.0.0/16"]]["10.1.0.0/16","10.2.0.0/16"]not run yetsample