Coding questions · Figma
Validate an IPv4 address string
Evaluates string parsing and input-validation skills, including digit-only checks, delimiter-based splitting, numeric range enforcement, and edge-case handling. Commonly asked to assess attention to input formats and robustness in implementation; Category: Coding & Algorithms; Level: implementation-level (concrete string manipulation and validation).
Given a string s, determine whether it is a valid IPv4 address.
A valid IPv4 address:
- Has exactly 4 parts separated by dots (
.):x1.x2.x3.x4 - Each part is a base-10 integer in the range
[0, 255] - No extra characters are allowed (only digits and dots)
Examples:
- Valid:
"192.168.1.1","192.168.1.0" - Invalid:
"192.168.100.1.1"(5 parts),"192.1681.1"(missing dot separation)
Write a function that returns True if s is a valid IPv4 address and False otherwise.
Validate an IPv4 address string
Given a string s, determine whether it is a valid IPv4 address.
A valid IPv4 address:
- Has exactly 4 parts separated by dots (
.):x1.x2.x3.x4 - Each part is a base-10 integer in the range
[0, 255] - No extra characters are allowed (only digits and dots)
- A part may not have a leading zero (e.g.
01or00is invalid), and an empty part (e.g. from192.168..1) is invalid
Return true if s is a valid IPv4 address and false otherwise.
Examples:
- Valid:
"192.168.1.1","192.168.1.0","0.0.0.0","255.255.255.255" - Invalid:
"192.168.100.1.1"(5 parts),"192.1681.1"(missing dot separation),"256.1.1.1"(octet > 255),"192.168.01.1"(leading zero)
Constraints
- 1 <= len(s) <= 100 (s may also be empty, which is invalid)
- s consists of printable ASCII characters; only digits and '.' can appear in a valid address
- Octets must be in the range [0, 255] with no leading zeros
Hints
- Split the string on '.' and immediately reject anything that does not produce exactly 4 parts.
- For each part: reject if it is empty, contains a non-digit character, has a leading zero (length > 1 and starts with '0'), or its integer value exceeds 255.
- Using s.split('.') keeps empty parts (e.g. '192.168..1' yields a '' part), so an explicit emptiness check catches the double-dot case.