Restore IP Addresses
Reported by candidates from Motive's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Strip the IPv4 costume off this Motive question, reported in June 2025, and it's a tiny backtracking problem: choose where to cut a digit string into four chunks. Each chunk is 1 to 3 digits, 0 to 255, no leading zeros. That's it. If your OA invite is sitting in your inbox, this is one you can actually finish cleanly. The input is capped at 20 characters, so brute force is fine. The real risk is sloppy edge cases, not complexity. StealthCoder is there as a quiet safety net during the live OA if your mind goes blank on the recursion, but you should be able to rebuild this from scratch tonight.
The problem
Given a string digits containing only decimal digits, return every valid IPv4 address obtained by inserting exactly three dots. Return the addresses in lexicographic order. Each segment must be from 0 through 255 and cannot have a leading zero unless it is exactly 0. Function restoreIpAddresses(digits: String) → String[] Examples Example 1 digits = "25525511135" return = ["255.255.11.135","255.255.111.35"] Example 2 digits = "0000" return = ["0.0.0.0"] Constraints 1 <= digits.length <= 20.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is a depth-first search with two parameters: the current index and how many segments you've placed. At each step, try segment lengths 1, 2, and 3. Reject a segment if it runs past the end of the string, has a leading zero with length above 1, or exceeds 255. When you've placed four segments and consumed every digit, join with dots and save it. The common pitfall is forgetting to use all digits, so you emit addresses that drop a tail. Another is parsing '01' as valid. Order matters too: if you try lengths 1, 2, 3 in that order and walk left to right, results come out lexicographic naturally, but sort at the end anyway if you're unsure. Prune early when remaining characters can't fit the remaining segments. If the recursion slips away during the live OA, StealthCoder can surface a clean version fast.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Restore IP Addresses cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as restore ip addresses. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Motive's OA.
Motive reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Restore IP Addresses FAQ
How hard is Restore IP Addresses really?+
It's a medium on paper, but easy once you see it's backtracking over three cut points. Length is capped at 20, so there's no performance trap. Most failures come from leading-zero handling and forgetting to consume the whole string, not from the algorithm itself.
What's the trick to solving it fast?+
Write a recursive helper taking the start index and the segment count. Try lengths 1 to 3, validate each piece, recurse, and backtrack. At four segments, accept only if you're exactly at the end of the string. Everything else is validation details.
How do I handle leading zeros correctly?+
If a segment has more than one character and starts with '0', reject it. So '0' is fine, but '00', '01', and '012' are not. For '0000', the only answer is 0.0.0.0, which is a good test case to run before submitting.
Does the output need to be in lexicographic order?+
Yes, the problem asks for it. Iterating segment lengths from 1 up to 3 while moving left to right typically yields sorted output, but calling a sort on the result list is a cheap guarantee. Do that if you're short on time to verify.
How do I prepare for this in 48 hours?+
Code it from memory twice, once with string slicing and once with a path list. Then test on '25525511135', '0000', and a short string like '123' that returns an empty list. Also try a 20-digit input to confirm it doesn't blow up.