PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates string processing, strict input validation rules, and constrained combinatorial search skills (e.g., backtracking) for generating and verifying IPv4 addresses.

  • Medium
  • Flexport
  • Coding & Algorithms
  • Software Engineer

Validate and restore IPv4 addresses

Company: Flexport

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

Design and implement the following IPv4 tasks. a) Validate dotted IPv4: Write a function isValidIPv4 (s) that returns true if s is a valid dotted-decimal IPv4 address and false otherwise. Requirements: - Exactly four decimal octets separated by three dots. - Each octet is digits only and represents an integer in [0, 255]. - No leading zeros unless the octet is exactly "0". - No plus/minus signs, whitespace, or extra characters. Examples: "255.255.11.135" -> true; "-1.255.11.135" -> false; "01.2.3.4" -> false; "256.0.0.1" -> false. b) Validate undelimited IPv4 possibility: Given a string s consisting only of digits (no dots), return true if it can be segmented into a valid IPv4 by inserting three dots under the same rules as (a); otherwise return false. Examples: "25525511135" -> true (e.g., 255.255.11. 135); "0002555" -> true (e.g., 0.0.255. 55); "123" -> false. c) Restore all valid IPv4 addresses: Given a string s of digits, return all valid IPv4 addresses formed by inserting three dots under the rules above. Return in any order without duplicates. Example: "25525511135" -> ["255.255.11.135", "255.255.111.35"]. Constraints for (b) and (c): 1 <= s.length <= 12. Aim for an efficient solution (typical approach uses DFS/backtracking).

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

Write a function `isValidIPv4(s)` that returns `true` if `s` is a valid dotted-decimal IPv4 address and `false` otherwise. A string is a valid IPv4 address when ALL of the following hold: - It has exactly four decimal octets separated by exactly three dots. - Each octet contains digits only and represents an integer in the range [0, 255]. - An octet has no leading zeros unless it is exactly "0" (so "0" is valid but "00" and "01" are not). - There are no plus/minus signs, no whitespace, and no extra characters anywhere in the string. Examples: - "255.255.11.135" -> true - "-1.255.11.135" -> false (minus sign) - "01.2.3.4" -> false (leading zero) - "256.0.0.1" -> false (octet out of range)

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

  1. Split on '.' and immediately reject if you do not get exactly four parts.
  2. Use a digits-only check (e.g. str.isdigit()) to rule out signs, spaces, and empty octets in one shot.
  3. 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?

Given a string `s` consisting only of digits (no dots), return `true` if it can be segmented into a valid IPv4 address by inserting exactly three dots, using the same rules as a standard dotted-decimal IPv4 address; otherwise return `false`. Rules for each resulting octet: - Contains digits only and represents an integer in [0, 255]. - No leading zeros unless the octet is exactly "0". You must use every character of `s` and produce exactly four octets. Examples: - "25525511135" -> true (e.g., 255.255.11.135) - "0025555" -> true (e.g., 0.0.255.55) - "123" -> false (cannot be split into four non-empty octets) Constraint: 1 <= s.length <= 12.

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

  1. Each octet is 1 to 3 characters long, so an early length check (4 <= len <= 12) prunes impossible inputs.
  2. Backtrack over octet lengths 1, 2, 3; stop a branch as soon as the candidate octet is invalid (leading zero or > 255).
  3. 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

Given a string `s` of digits, return a list of all valid IPv4 addresses that can be formed by inserting exactly three dots into `s`. You must use every digit of `s`, and each resulting address must follow standard IPv4 rules: - Exactly four octets, each an integer in [0, 255]. - No leading zeros in an octet unless the octet is exactly "0". Return the addresses in any order, with no duplicates. (Sorting the result gives a deterministic order.) Example: - "25525511135" -> ["255.255.11.135", "255.255.111.35"] Constraint: 1 <= s.length <= 12.

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

  1. Backtrack by trying octet lengths of 1, 2, and 3 at each of the four positions.
  2. Prune a branch immediately when an octet has a leading zero (and length > 1) or exceeds 255.
  3. Only record a candidate when you have placed four octets AND consumed the entire string; sort the collected results for a stable output order.
Last updated: Jun 26, 2026

Related Coding Questions

  • Solve Two Robbery Optimization Variants - Flexport (medium)

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.