Classify a String under the IPv4 and IPv6 Rules

Quick Overview

Classify an address string using the exact IPv4 and full eight-group IPv6 rules, including separator counts, leading zeros, decimal bounds and hexadecimal characters.

Classify a String under the IPv4 and IPv6 Rules

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

# Classify a String under the IPv4 and IPv6 Rules Given `queryIP`, return exactly `"IPv4"`, `"IPv6"` or `"Neither"` according to the following rules. An IPv4 address has exactly four decimal groups separated by single dots. Each group represents a value from 0 through 255 and has no leading zeros, except that `"0"` itself is allowed. An IPv6 address for this exercise has exactly eight groups separated by single colons. Each group has one to four hexadecimal characters: digits or letters `a` through `f` in either case. Leading zeros within these groups are allowed. The abbreviated `::` form and mixed dotted/colon forms do not satisfy this exercise's format. ### Interface `class Solution { public: string validIPAddress(string queryIP); };` ### Example 1 ```text Input: queryIP = "203.0.113.7" Output: "IPv4" ``` There are four decimal groups, each within range and without a leading zero. ### Example 2 ```text Input: queryIP = "1:2:3:4:5:6:7:00000" Output: "Neither" ``` The final hexadecimal group has five characters, exceeding the permitted length. ### Constraints - `queryIP` contains only English letters, digits, dots and colons. - No additional string-length bound is specified. ### What a Strong Answer Covers - Exact group counts, nonempty groups and permitted characters. - The different leading-zero rules and decimal range restriction. - Leading or trailing separators, mixed separators and invalid hexadecimal letters. - Safe handling of long invalid inputs without numeric conversion overflow. ```hint Local and whole-string rules Which rules apply to an individual group, and which depend on the complete address? ```

Overview: Classify an address string using the exact IPv4 and full eight-group IPv6 rules, including separator counts, leading zeros, decimal bounds and hexadecimal characters.

Amazon logo
Amazon
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Classify a String under the IPv4 and IPv6 Rules

Given queryIP, return exactly "IPv4", "IPv6" or "Neither" according to the following rules.

An IPv4 address has exactly four decimal groups separated by single dots. Each group represents a value from 0 through 255 and has no leading zeros, except that "0" itself is allowed.

An IPv6 address for this exercise has exactly eight groups separated by single colons. Each group has one to four hexadecimal characters: digits or letters a through f in either case. Leading zeros within these groups are allowed. The abbreviated :: form and mixed dotted/colon forms do not satisfy this exercise's format.

Interface

class Solution { public: string validIPAddress(string queryIP); };

Example 1

Input: queryIP = "203.0.113.7"
Output: "IPv4"

There are four decimal groups, each within range and without a leading zero.

Example 2

Input: queryIP = "1:2:3:4:5:6:7:00000"
Output: "Neither"

The final hexadecimal group has five characters, exceeding the permitted length.

Constraints

  • queryIP contains only English letters, digits, dots and colons.
  • No additional string-length bound is specified.

What a Strong Answer Covers Guidance

  • Exact group counts, nonempty groups and permitted characters.
  • The different leading-zero rules and decimal range restriction.
  • Leading or trailing separators, mixed separators and invalid hexadecimal letters.
  • Safe handling of long invalid inputs without numeric conversion overflow.