← Questions
Validate an IPv4 address
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Figma's technical phone screen, last asked Jan 2026. It tests strings, parsing and validation.
Given a string, decide whether it is a valid IPv4 address: four decimal numbers separated by dots, each from 0 to 255, with no leading zeros and no other characters.
Interface
Unless you agree on a different interface with the interviewer, assume:
is_valid_ipv4(s: str) -> bool- Valid means: exactly four parts separated by
.; each part is one or more digits0-9; its value is at most 255; a part longer than one digit doesn't start with0.
Example
is_valid_ipv4("192.168.1.1") -> True
is_valid_ipv4("0.0.0.0") -> True
is_valid_ipv4("255.255.255.255") -> True
is_valid_ipv4("192.168.100.1.1") -> False # five parts
is_valid_ipv4("192.1681.1") -> False # three parts
is_valid_ipv4("256.1.1.1") -> False # 256 > 255
is_valid_ipv4("192.168.01.1") -> False # leading zero
is_valid_ipv4("192.168..1") -> False # empty part
is_valid_ipv4("1.2.3.4 ") -> False # trailing space
Constraints
-
0 <= len(s) <= 100;sis printable ASCII and may be empty.
Clarifying questions
- Are leading zeros allowed (
01)? Is0alone valid? - Should surrounding whitespace be trimmed or rejected?
- Are signs (
+1) or non-ASCII digits possible in the input? - Should I return a reason for rejection, or only a boolean?
What a strong answer covers
- Split on
.and require exactly four parts;splitkeeps empty parts, so"1..2.3"is caught by an empty-part check. - For each part: non-empty, ASCII digits only (Python's
str.isdigit()accepts other Unicode digits, so check'0' <= c <= '9'), no leading zero, value at most 255. Length at most 3 before converting avoids huge numbers. - O(n) time, O(1) extra space beyond the split.
- Tests for every rule above, plus the empty string and a lone
..
Follow-ups
- Validate IPv6 as well, and return which kind it is.
- Without using
split, parse in a single pass with a small state machine. - Given a string of digits only, return every valid IPv4 address you can make by inserting three dots.
Where it's asked
- Figma · Technical phone screen · Last asked Jan 202660 min · Pair programming in your own language against tests the interviewer provides
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
