Quick 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.

Classify a string as an IPv4 address, an IPv6 address or neither

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given a string `query_ip`, return `"IPv4"` if it is a valid IPv4 address, `"IPv6"` if it is a valid IPv6 address, or `"Neither"` if it is not a correct IP address of either type. A valid IPv4 address has the form `"x1.x2.x3.x4"` where `0 <= xi <= 255` and `xi` cannot contain leading zeros. For example, `"192.168.1.1"` and `"192.168.1.0"` are valid IPv4 addresses, while `"192.168.01.1"`, `"192.168.1.00"` and `"192.168@1.1"` are invalid IPv4 addresses. A valid IPv6 address has the form `"x1:x2:x3:x4:x5:x6:x7:x8"` where: - `1 <= xi.length <= 4` - `xi` is a hexadecimal string, which may contain digits, lowercase English letters (`'a'` to `'f'`) and uppercase English letters (`'A'` to `'F'`). - Leading zeros are allowed in IPv6. For example, `"2001:0db8:85a3:0000:0000:8a2e:0370:7334"` and `"2001:db8:85a3:0:0:8A2e:0370:7334"` are valid IPv6 addresses, while `"2001:0db8:85a3::8A2e:037j:7334"` and `"02001:0db8:85a3:0000:0000:8a2e:0370:7334"` are invalid IPv6 addresses. ### Function Signature ```python def valid_ip_address(query_ip: str) -> str: ``` ### Rules - Return exactly one of the strings `"IPv4"`, `"IPv6"` or `"Neither"`, with that capitalization. - No group may be empty, so a leading, trailing or doubled separator makes the address invalid. The shortened IPv6 form with `::` is not accepted. - An IPv4 group equal to `"0"` is valid. A group that starts with `0` and has more digits, such as `"01"` or `"00"`, is not. ### Constraints - `query_ip` consists only of English letters, digits and the characters `'.'` and `':'`. ### Examples #### Example 1 ```text Input: query_ip = "172.16.254.1" Output: "IPv4" ``` It has 4 groups separated by dots (172, 16, 254 and 1). Each is in the range 0 to 255 and none has a leading zero. #### Example 2 ```text Input: query_ip = "2001:0db8:85a3:0:0:8A2e:0370:7334" Output: "IPv6" ``` It has 8 groups separated by colons, and each group is 1 to 4 hexadecimal characters. Mixed case and leading zeros such as `0db8` are allowed in IPv6. #### Example 3 ```text Input: query_ip = "256.256.256.256" Output: "Neither" ``` It has the IPv4 shape, but every group is above 255. It contains no colons, so it cannot be an IPv6 address either.

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.

Given a string `query_ip`, return `"IPv4"` if it is a valid IPv4 address, `"IPv6"` if it is a valid IPv6 address, or `"Neither"` if it is not a correct IP address of either type. A valid IPv4 address has the form `"x1.x2.x3.x4"`: four groups separated by dots, where `0 <= xi <= 255` and `xi` cannot contain leading zeros. A group equal to `"0"` is valid. A group that starts with `0` and has more digits, such as `"01"` or `"00"`, is not. For example, `"192.168.1.1"` and `"192.168.1.0"` are valid IPv4 addresses, while `"192.168.01.1"` and `"192.168.1.00"` are invalid IPv4 addresses. A valid IPv6 address has the form `"x1:x2:x3:x4:x5:x6:x7:x8"`: eight groups separated by colons, where: - `1 <= xi.length <= 4` - `xi` is a hexadecimal string, which may contain digits, lowercase English letters (`'a'` to `'f'`) and uppercase English letters (`'A'` to `'F'`). - Leading zeros are allowed in IPv6. For example, `"2001:0db8:85a3:0000:0000:8a2e:0370:7334"` and `"2001:db8:85a3:0:0:8A2e:0370:7334"` are valid IPv6 addresses, while `"2001:0db8:85a3::8A2e:037j:7334"` and `"02001:0db8:85a3:0000:0000:8a2e:0370:7334"` are invalid IPv6 addresses. No group may be empty, so a leading, trailing or doubled separator makes the address invalid. The shortened IPv6 form with `::` is not accepted. Return exactly one of the strings `"IPv4"`, `"IPv6"` or `"Neither"`, with that capitalization. The input and the output are single strings, so no numeric value crosses the function boundary. A run of digits inside `query_ip` can still be long enough that its numeric value exceeds 2^31 - 1; such a group is above 255 and is not a valid IPv4 group. ### Example 1 ```text Input: query_ip = "172.16.254.1" Output: "IPv4" ``` It has 4 groups separated by dots (172, 16, 254 and 1). Each is in the range 0 to 255 and none has a leading zero. ### Example 2 ```text Input: query_ip = "2001:0db8:85a3:0:0:8A2e:0370:7334" Output: "IPv6" ``` It has 8 groups separated by colons, and each group is 1 to 4 hexadecimal characters. Mixed case and leading zeros such as `0db8` are allowed in IPv6. ### Constraints - `1 <= query_ip.length <= 1000` - `query_ip` consists only of English letters (`a-z`, `A-Z`), digits (`0-9`) and the characters `'.'` and `':'`.

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

  1. An IPv4 group holds only decimal digits and an IPv6 group holds only hexadecimal characters. Consider which separators each format can contain.
  2. 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.
  3. 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.

Classify a String as IPv4, IPv6 or Neither

mediumGeneralSoftware Engineer

Given a string query_ip, return "IPv4" if it is a valid IPv4 address, "IPv6" if it is a valid IPv6 address, or "Neither" if it is not a correct IP address of either type.

A valid IPv4 address has the form "x1.x2.x3.x4": four groups separated by dots, where 0 <= xi <= 255 and xi cannot contain leading zeros. A group equal to "0" is valid. A group that starts with 0 and has more digits, such as "01" or "00", is not. For example, "192.168.1.1" and "192.168.1.0" are valid IPv4 addresses, while "192.168.01.1" and "192.168.1.00" are invalid IPv4 addresses.

A valid IPv6 address has the form "x1:x2:x3:x4:x5:x6:x7:x8": eight groups separated by colons, where:

  • 1 <= xi.length <= 4
  • xi is a hexadecimal string, which may contain digits, lowercase English letters ('a' to 'f') and uppercase English letters ('A' to 'F').
  • Leading zeros are allowed in IPv6.

For example, "2001:0db8:85a3:0000:0000:8a2e:0370:7334" and "2001:db8:85a3:0:0:8A2e:0370:7334" are valid IPv6 addresses, while "2001:0db8:85a3::8A2e:037j:7334" and "02001:0db8:85a3:0000:0000:8a2e:0370:7334" are invalid IPv6 addresses.

No group may be empty, so a leading, trailing or doubled separator makes the address invalid. The shortened IPv6 form with :: is not accepted.

Return exactly one of the strings "IPv4", "IPv6" or "Neither", with that capitalization. The input and the output are single strings, so no numeric value crosses the function boundary. A run of digits inside query_ip can still be long enough that its numeric value exceeds 2^31 - 1; such a group is above 255 and is not a valid IPv4 group.

Example 1

Input: query_ip = "172.16.254.1"
Output: "IPv4"

It has 4 groups separated by dots (172, 16, 254 and 1). Each is in the range 0 to 255 and none has a leading zero.

Example 2

Input: query_ip = "2001:0db8:85a3:0:0:8A2e:0370:7334"
Output: "IPv6"

It has 8 groups separated by colons, and each group is 1 to 4 hexadecimal characters. Mixed case and leading zeros such as 0db8 are allowed in IPv6.

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

Example 1
Input
('172.16.254.1',)
Output
'IPv4'
Notes
Source Example 1: four decimal groups 172, 16, 254 and 1, each in range with no leading zero. Input length 12.
Example 2
Input
('2001:0db8:85a3:0:0:8A2e:0370:7334',)
Output
'IPv6'
Notes
Source Example 2: eight hex groups with mixed case (8A2e) and leading zeros (0db8, 0370), all allowed in IPv6. Input length 33.

Constraints

  • •`1 <= query_ip.length <= 1000`
  • •`query_ip` consists only of English letters (`a-z`, `A-Z`), digits (`0-9`) and the characters `'.'` and `':'`.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def valid_ip_address(query_ip) -> str:
Sample call
valid_ip_address('172.16.254.1')
Language
Case 1
Input
('172.16.254.1',)
Expected
'IPv4'
Case 2
Input
('2001:0db8:85a3:0:0:8A2e:0370:7334',)
Expected
'IPv6'

Browse More Questions

Show the approach

Approach

Test IPv4 first. Split query_ip on '.' and keep empty pieces. The string is IPv4 exactly when there are four pieces and every piece is 1 to 3 characters long, uses only the digits 0 to 9, does not start with 0 unless it is exactly "0", and has a value of at most 255. Otherwise split on ':' and return IPv6 exactly when there are eight pieces and every piece is 1 to 4 characters long and uses only 0-9, a-f and A-F. If neither test passes, return Neither.

The answer is unique: an IPv4 piece holds only digits, so a string that passes the IPv4 test contains no ':', and '.' is not a hexadecimal character, so a string that passes the IPv6 test contains no '.'. At most one test can pass.

Empty groups: splitting keeps empty pieces, so a leading or trailing separator, two separators in a row and the :: shorthand each produce an empty piece or change the piece count, and the length check rejects an empty piece. Java's String.split drops trailing empty strings, so the Java reference splits by hand.

No overflow: the length check runs before any digits are accumulated, so an IPv4 piece contributes at most 999 and a long run of digits never reaches an integer conversion.

Edge cases covered: a single character, separators only, a lone 0 versus 00 and 01, the values 255 and 256, uppercase hex letters (valid in IPv6, invalid in IPv4), a 5-character IPv6 group, and an IPv6 prefix with a dotted tail.

Time complexity:
O(n)
Space complexity:
O(n)