Classify a string as an IPv4 address, an IPv6 address or neither
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Other
Overview: Classify a string as a valid IPv4 address, a valid IPv6 address, or neither, under strict rules for group counts, value ranges, leading zeros and hexadecimal characters. Tests careful string parsing and handling of edge cases such as empty groups and stray separators.
Constraints
- `1 <= query_ip.length <= 1000`
- `query_ip` consists only of English letters (`a-z`, `A-Z`), digits (`0-9`) and the characters `'.'` and `':'`.
Examples
Input: ('172.16.254.1',)
Expected Output: 'IPv4'
Explanation: Source Example 1: four decimal groups 172, 16, 254 and 1, each in range with no leading zero. Input length 12.
Input: ('2001:0db8:85a3:0:0:8A2e:0370:7334',)
Expected Output: 'IPv6'
Explanation: Source Example 2: eight hex groups with mixed case (8A2e) and leading zeros (0db8, 0370), all allowed in IPv6. Input length 33.
Hints
- An IPv4 group holds only decimal digits and an IPv6 group holds only hexadecimal characters. Consider which separators each format can contain.
- List the ways a group can end up empty: a separator at the start, a separator at the end, or two separators next to each other.
- For an IPv4 group, think about what you can rule out from its length and its first character before you look at its numeric value.