Validate and restore IPv4 addresses
Company: Flexport
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: This question evaluates string processing, strict input validation rules, and constrained combinatorial search skills (e.g., backtracking) for generating and verifying IPv4 addresses.
Validate Dotted-Decimal IPv4 Address
Constraints
- The input is an arbitrary string; it may contain non-digit characters, signs, or whitespace.
- A valid address has exactly four octets and exactly three dots.
- Each octet is in [0, 255] with no leading zeros (except the single character "0").
- Empty octets (e.g. "1..2.3") and missing/extra dots are invalid.
Examples
Input: ("255.255.11.135",)
Expected Output: True
Explanation: All four octets are in range with no leading zeros.
Input: ("-1.255.11.135",)
Expected Output: False
Explanation: The first octet contains a minus sign, which is not allowed.
Input: ("01.2.3.4",)
Expected Output: False
Explanation: The octet '01' has a leading zero.
Input: ("256.0.0.1",)
Expected Output: False
Explanation: 256 is outside the [0, 255] range.
Input: ("0.0.0.0",)
Expected Output: True
Explanation: A single '0' octet is allowed.
Input: ("1.2.3",)
Expected Output: False
Explanation: Only three octets; need exactly four.
Input: ("1.2.3.4.5",)
Expected Output: False
Explanation: Five octets is too many.
Input: ("255.255.255.255",)
Expected Output: True
Explanation: Maximum valid address.
Input: (" 1.2.3.4",)
Expected Output: False
Explanation: Leading whitespace makes the first octet ' 1', not digits-only.
Input: ("1.2.3.+4",)
Expected Output: False
Explanation: The plus sign makes '+4' not digits-only.
Input: ("",)
Expected Output: False
Explanation: Empty string splits into one part, not four.
Input: ("00.0.0.0",)
Expected Output: False
Explanation: '00' is a multi-character octet starting with zero.
Hints
- Split on '.' and immediately reject if you do not get exactly four parts.
- Use a digits-only check (e.g. str.isdigit()) to rule out signs, spaces, and empty octets in one shot.
- Reject any multi-character octet whose first character is '0' to enforce the no-leading-zero rule, then compare the integer value against 255.
Can a Digit String Be Segmented into a Valid IPv4?
Constraints
- 1 <= s.length <= 12.
- s consists of digit characters only.
- Every character must be consumed and the result must contain exactly four octets.
- A string shorter than 4 or longer than 12 characters can never form a valid IPv4 address.
Examples
Input: ("25525511135",)
Expected Output: True
Explanation: Splits as 255.255.11.135.
Input: ("0025555",)
Expected Output: True
Explanation: Splits as 0.0.255.55 (each leading '0' is a standalone octet).
Input: ("123",)
Expected Output: False
Explanation: Only three characters cannot fill four non-empty octets.
Input: ("0000",)
Expected Output: True
Explanation: Splits as 0.0.0.0.
Input: ("1111",)
Expected Output: True
Explanation: Splits as 1.1.1.1.
Input: ("256256256256",)
Expected Output: False
Explanation: Every 3-digit group is 256, which exceeds 255, and no other split fits.
Input: ("255255255255",)
Expected Output: True
Explanation: Splits as 255.255.255.255.
Input: ("1",)
Expected Output: False
Explanation: Too short to form four octets.
Input: ("1234567890123",)
Expected Output: False
Explanation: Length 13 exceeds the 12-character maximum.
Input: ("0000256",)
Expected Output: False
Explanation: The three leading zeros must be standalone octets, leaving '256' which is > 255.
Input: ("00000",)
Expected Output: False
Explanation: Five zeros need four octets, but every '0' must be standalone and 'four 0 octets' only consume four characters.
Input: ("101010101",)
Expected Output: True
Explanation: Can be segmented, e.g. 10.10.10.101 — all octets are valid.
Hints
- Each octet is 1 to 3 characters long, so an early length check (4 <= len <= 12) prunes impossible inputs.
- Backtrack over octet lengths 1, 2, 3; stop a branch as soon as the candidate octet is invalid (leading zero or > 255).
- After placing three dots, the fourth octet is simply the remaining suffix — accept only if that suffix is itself a valid octet.
Restore All Valid IPv4 Addresses
Constraints
- 1 <= s.length <= 12.
- s consists of digit characters only.
- Every digit must be used; each address has exactly four octets.
- The returned list contains no duplicate addresses.
Examples
Input: ("25525511135",)
Expected Output: ["255.255.11.135", "255.255.111.35"]
Explanation: The two ways to place dots that keep every octet <= 255 without leading zeros.
Input: ("0000",)
Expected Output: ["0.0.0.0"]
Explanation: Each zero must stand alone, giving a single address.
Input: ("123",)
Expected Output: []
Explanation: Three characters cannot fill four octets.
Input: ("101023",)
Expected Output: ["1.0.10.23", "1.0.102.3", "10.1.0.23", "10.10.2.3", "101.0.2.3"]
Explanation: All five valid dot placements, sorted lexicographically.
Input: ("1111",)
Expected Output: ["1.1.1.1"]
Explanation: Only one segmentation fits.
Input: ("255255255255",)
Expected Output: ["255.255.255.255"]
Explanation: Each octet must be exactly '255'.
Input: ("00000",)
Expected Output: []
Explanation: Five zeros cannot form four standalone-'0' octets that consume all characters.
Input: ("1234567890123",)
Expected Output: []
Explanation: Length 13 exceeds the 12-character maximum.
Input: ("0025555",)
Expected Output: ["0.0.255.55"]
Explanation: The leading zeros are forced standalone, leaving the unique split 0.0.255.55.
Hints
- Backtrack by trying octet lengths of 1, 2, and 3 at each of the four positions.
- Prune a branch immediately when an octet has a leading zero (and length > 1) or exceeds 255.
- Only record a candidate when you have placed four octets AND consumed the entire string; sort the collected results for a stable output order.