Restore Every Valid IP Address
Company: TikTok
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: Restore every valid IPv4 address by inserting three dots into a string of digits. The task defines segment bounds, leading-zero rules, exact digit use, lexicographic output, impossible cases, and two examples for later cross-language console verification.
Read the full TikTok Software Engineer interview experience this question came from
Constraints
- 1 <= len(s) <= 20
- s contains only decimal digits.
- Every output uses all input digits exactly once and in order.
- Each segment is in [0, 255] and has no multi-digit leading zero.
- Return unique addresses in ascending lexicographic order.
Examples
Input: ('25525511135',)
Expected Output: ['255.255.11.135', '255.255.111.35']
Explanation: Both source-example addresses have four valid segments and the sorted order is deterministic.
Input: ('0000',)
Expected Output: ['0.0.0.0']
Explanation: Every zero must stand alone.
Hints
- Each of the four segments contains between one and three digits.
- Prune when the remaining digits cannot fill the remaining segments within those length bounds.