As reported: Yahoo Software Engineer 2 final loop (2025), round 2: easy code to validate an IPv4 address, extended to validate IPv4/CIDR; the same round then moved to designing a movie ticket booking system.
Implement validate(s), which classifies a string as:
"IPv4"— a valid IPv4 address: exactly four decimal parts separated by., each part an integer from0to255with no leading zeros ("0"is fine,"01"is not) and only digits."CIDR"— a valid IPv4 address followed by/and a prefix length: an integer from0to32, digits only, with no leading zeros ("/0"is fine,"/08"is not)."Neither"— anything else.
No spaces or other characters are allowed anywhere.
Example 1
s = "172.16.254.1"
Output: "IPv4"
Example 2
s = "10.0.0.0/8"
Output: "CIDR"
Example 3
s = "192.168.01.1"
Output: "Neither"
Leading zero in 01.
Example 4
s = "192.168.1.0/33"
Output: "Neither"
The prefix length must be at most 32.
Constraints: 0 ≤ s.length ≤ 100; s consists of printable ASCII characters.
Code runner format: one JSON line: s. Implement validate(s).