# The IP Validator

> Real and fake, mixed together.

Canonical URL: <https://datadriven.io/problems/the_ip_validator>

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a string, return True if it is a valid IPv4 address: exactly four decimal octets separated by dots, each in range 0..255, with no leading zeros (except '0' itself).

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **string validation with multiple rules**, a pattern common in data quality and input sanitization. It probes whether a candidate can enumerate all validation criteria and check them systematically.

---

### Break down the requirements

#### Step 1: Split by dots and check for exactly 4 parts

A valid IPv4 address has exactly 4 octets separated by dots.

#### Step 2: Validate each octet

Each must be a numeric string representing an integer between 0 and 255.

#### Step 3: Check for leading zeros

Octets like `'01'` or `'00'` are invalid (except `'0'` itself).

---

### The solution

**Multi-rule IPv4 validation**

```python
def is_valid_ipv4(s: str) -> bool:
    parts = s.split('.')
    if len(parts) != 4:
        return False
    for part in parts:
        if not part:
            return False
        if not part.isdigit():
            return False
        if len(part) > 1 and part[0] == '0':
            return False
        num = int(part)
        if num < 0 or num > 255:
            return False
    return True
```

> **Time and Space Complexity**
>
> **Time:** O(1). The string has at most 15 characters (255.255.255.255).
> 
> **Space:** O(1). Only the split parts (at most 4).

> **Interviewers Watch For**
>
> Whether you check for leading zeros. This is the most commonly missed validation rule and a frequent source of real-world bugs.

> **Common Pitfall**
>
> Using `int()` directly on parts that might contain non-numeric characters. `'12a'.isdigit()` returns `False` in Python, so checking `isdigit()` first prevents `ValueError`.

---

## Common follow-up questions

- How would you validate IPv6 addresses? _(Tests hexadecimal validation, colon separators, and shorthand expansion.)_
- What about CIDR notation like 192.168.1.0/24? _(Tests splitting on `/` first, then validating the IP and prefix length separately.)_
- Would you use regex for this in production? _(Tests tradeoff: regex is more concise but harder to debug and extend.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_ip_validator)
- [Python Interview Questions](https://datadriven.io/python-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.