Validate an IPv4 address string
Company: Figma
Role: Data Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: 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).
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
Examples
Input: ("192.168.1.1",)
Expected Output: True
Explanation: Standard valid address: 4 parts, each in [0,255].
Input: ("192.168.1.0",)
Expected Output: True
Explanation: 0 is a valid octet value.
Input: ("0.0.0.0",)
Expected Output: True
Explanation: Minimum address; single-digit zeros are allowed.
Input: ("255.255.255.255",)
Expected Output: True
Explanation: Maximum address; 255 is the upper bound.
Input: ("192.168.100.1.1",)
Expected Output: False
Explanation: Five parts — an IPv4 address must have exactly four.
Input: ("192.1681.1",)
Expected Output: False
Explanation: Only three parts because a dot is missing.
Input: ("256.1.1.1",)
Expected Output: False
Explanation: 256 exceeds the maximum octet value of 255.
Input: ("1.1.1",)
Expected Output: False
Explanation: Only three parts.
Input: ("",)
Expected Output: False
Explanation: Empty string splits into one empty part, not four.
Input: ("192.168.01.1",)
Expected Output: False
Explanation: '01' has a leading zero, which is invalid.
Input: ("192.168..1",)
Expected Output: False
Explanation: Consecutive dots produce an empty octet, which is invalid.
Input: ("abc.def.ghi.jkl",)
Expected Output: False
Explanation: Parts must be base-10 digits, not letters.
Input: ("-1.0.0.0",)
Expected Output: False
Explanation: '-1' is not all digits and is out of range.
Input: ("192.168.1.1 ",)
Expected Output: False
Explanation: A trailing space makes the last part '1 ', which is not all digits.
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.