The Restore IP Addresses interview question gives you a string of digits and asks you to return all possible valid IPv4 addresses that can be formed by inserting exactly three dots into the string. A valid IPv4 address has four parts (octets), each being a number between 0 and 255, with no leading zeros (except "0" itself). The digit string must be fully consumed.
This problem is asked at Apple, Cisco, Goldman Sachs, Microsoft, Meta, Amazon, Google, Bloomberg, and Adobe because it is a canonical backtracking problem with clear pruning conditions. IP address validation is directly relevant to networking software, firewall configuration, and log parsing. It tests systematic enumeration, constraint checking, and recognizing when to prune the search tree early.
The pattern is backtracking with pruning. Use a recursive function that tracks the current position in the string, the current list of segments, and the number of segments formed. At each step, try taking 1, 2, or 3 digits as the next segment. Prune if: the segment has leading zeros (and length > 1), the segment value exceeds 255, or there aren't enough remaining digits to fill the remaining segments. When exactly 4 segments are formed and all digits are consumed, add the address to the result.
Input: "25525511135"
Backtrack:
Result: ["255.255.11.135", "255.255.111.35"].
"01" is invalid).For the Restore IP Addresses coding problem, the backtracking and string interview pattern is standard. List your pruning conditions before coding: leading zeros, value > 255, invalid remaining length. Interviewers at Cisco and Palo Alto Networks may ask you to extend this to IPv6 — show you can adapt the constraint checks. Practice clearly separating the "is this segment valid?" check from the "have we completed a valid address?" check.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| The k-th Lexicographical String of All Happy Strings of Length n | Medium | Solve | |
| Split Array into Fibonacci Sequence | Medium | Solve | |
| Additive Number | Medium | Solve | |
| Palindrome Partitioning | Medium | Solve | |
| Letter Case Permutation | Medium | Solve |