Validate an IPv4 address string
Problem Description
Given a string s, determine whether it represents a valid IPv4 address.
Definition of a Valid IPv4 Address
A string s is considered a valid IPv4 address if it adheres to the following rules:
- It must consist of exactly four numerical parts.
- These four parts must be separated by single dot (
.) characters. - Each part must be a base-10 integer.
- Each integer part must fall within the inclusive range of
0to255. - Each integer part must not have leading zeros (e.g., "01" is invalid, but "0" is valid).
- The string must not contain any characters other than digits (
0-9) and dots (.).
Examples
- Valid:
"192.168.1.1","192.168.1.0","0.0.0.0","255.255.255.255" - Invalid:
"192.168.100.1.1"(too many parts),"192.1681.1"(missing dot separation),"192.168.1.256"(part out of range),"192.168.01.1"(leading zero),"192.168.1.1."(trailing dot),".1.1.1.1"(leading dot),"192.168.1.a"(invalid character),""(empty string),"1.1..1.1"(multiple dots).
Task
Implement a function that takes a string s as input and returns True if s is a valid IPv4 address, and False otherwise.
AI interview prep
Core dilemma
Ensuring robust and efficient string parsing and validation against a strict set of rules, while handling numerous edge cases gracefully, especially for data quality in a Data Engineer context.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Validate an IPv4 address string
Problem Description
Given a string s, determine whether it represents a valid IPv4 address.
Definition of a Valid IPv4 Address
A string s is considered a valid IPv4 address if it adheres to the following rules:
- It must consist of exactly four numerical parts.
- These four parts must be separated by single dot (
.) characters. - Each part must be a base-10 integer.
- Each integer part must fall within the inclusive range of
0to255. - Each integer part must not have leading zeros (e.g., "01" is invalid, but "0" is valid).
- The string must not contain any characters other than digits (
0-9) and dots (.).
Examples
- Valid:
"192.168.1.1","192.168.1.0","0.0.0.0","255.255.255.255" - Invalid:
"192.168.100.1.1"(too many parts),"192.1681.1"(missing dot separation),"192.168.1.256"(part out of range),"192.168.01.1"(leading zero),"192.168.1.1."(trailing dot),".1.1.1.1"(leading dot),"192.168.1.a"(invalid character),""(empty string),"1.1..1.1"(multiple dots).
Task
Implement a function that takes a string s as input and returns True if s is a valid IPv4 address, and False otherwise.