Parse and expand an IPv6 address
IPv6 Address Parser and Normalizer
Implement a function that parses and normalizes an IPv6 address string. You will be given a string s which may represent an IPv6 address using standard notation.
IPv6 Standard Notation Rules:
- An IPv6 address consists of up to 8 groups, known as "hextets", separated by colons (
:). - Each hextet must contain between 1 and 4 hexadecimal digits (0-9, a-f, A-F).
- A single zero-compression
::sequence may appear at most once within the address. This::represents one or more consecutive0000hextets.
Task:
- If the input string
sis a valid IPv6 address according to the rules above, return its fully expanded canonical form. This canonical form must consist of exactly 8 hextets, each represented by 4 lowercase hexadecimal digits, joined by colons (:). - If
sdoes not represent a valid IPv6 address under these rules, return the string"Invalid".
Examples:
"2001:db8::1"→"2001:0db8:0000:0000:0000:0000:0000:0001""::"→"0000:0000:0000:0000:0000:0000:0000:0000""1:2:3:4:5:6:7:8"→"0001:0002:0003:0004:0005:0006:0007:0008""1::2::3"→"Invalid"(multiple::sequences are not allowed)"1:2:3:4:5:6:7:8:9"→"Invalid"(too many hextets)
Constraints:
- The length of the input string
sis at most 100 characters (|s| ≤ 100).
AI interview prep
Core dilemma
The central engineering conflict is robustly parsing a string with a complex, context-sensitive grammar (specifically the `::` zero-compression rule) while ensuring strict validation against multiple rules and producing a precise canonical output format. It's a test of meticulousness, edge case handling, and structured parsing logic.
2 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Parse and expand an IPv6 address
IPv6 Address Parser and Normalizer
Implement a function that parses and normalizes an IPv6 address string. You will be given a string s which may represent an IPv6 address using standard notation.
IPv6 Standard Notation Rules:
- An IPv6 address consists of up to 8 groups, known as "hextets", separated by colons (
:). - Each hextet must contain between 1 and 4 hexadecimal digits (0-9, a-f, A-F).
- A single zero-compression
::sequence may appear at most once within the address. This::represents one or more consecutive0000hextets.
Task:
- If the input string
sis a valid IPv6 address according to the rules above, return its fully expanded canonical form. This canonical form must consist of exactly 8 hextets, each represented by 4 lowercase hexadecimal digits, joined by colons (:). - If
sdoes not represent a valid IPv6 address under these rules, return the string"Invalid".
Examples:
"2001:db8::1"→"2001:0db8:0000:0000:0000:0000:0000:0001""::"→"0000:0000:0000:0000:0000:0000:0000:0000""1:2:3:4:5:6:7:8"→"0001:0002:0003:0004:0005:0006:0007:0008""1::2::3"→"Invalid"(multiple::sequences are not allowed)"1:2:3:4:5:6:7:8:9"→"Invalid"(too many hextets)
Constraints:
- The length of the input string
sis at most 100 characters (|s| ≤ 100).