Restore Addresses with K Segments
Reported by candidates from ByteDance's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The data structure here is the recursion stack, and ByteDance put it in an OA reported in September 2026. Restore Addresses with K Segments is the IPv4 restoration problem with the segment count turned into a parameter. If you've seen the classic, you're most of the way there. If you haven't, it's a backtracking problem in disguise. You pick 1 to 3 digits, check the value and leading zero rules, then recurse on the rest. The twist is that segmentCount isn't fixed at 4, so hardcoded loops will fail. StealthCoder is there as a safety net if you freeze during the live OA, but this one is learnable tonight.
The problem
You are given a string digits containing only decimal digits and an integer segmentCount. Insert exactly segmentCount - 1 dots so that every resulting segment: contains between one and three digits, represents an integer from 0 through 255, and has no leading zero unless the segment is exactly 0. Return every valid dotted address in lexicographic order. Return an empty array when no valid address exists. When segmentCount = 4, this is the standard restoration rule for IPv4 addresses; other values use the same segment rules. Function restoreAddresses(digits: String, segmentCount: int) → String[] Examples Example 1 digits = "25525511135" segmentCount = 4 return = ["255.255.11.135","255.255.111.35"] Both outputs have four valid segments, and no other placement satisfies the value and leading-zero rules. Example 2 digits = "010010" segmentCount = 4 return = ["0.10.0.10","0.100.1.0"] A segment beginning with 0 must be exactly 0, which eliminates placements such as 01. Example 3 digits = "1234" segmentCount = 2 return = ["1.234","12.34","123.4"] With two segments, the dot may follow the first, second, or third digit, and every resulting segment remains at most 255. Constraints 1 ≤ digits.length ≤ 30. digits contains only decimal digits. 1 ≤ segmentCount ≤ 10. The total number of returned characters fits in memory.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is backtracking with state (start index, segments left, current path). At each step, try lengths 1, 2, and 3. Reject a segment if it runs past the string, if it has a leading zero and length above 1, or if its value exceeds 255. When segments left hits 0, accept only if you've consumed every digit. Prune early: if the remaining digits are fewer than segments left, or more than 3 times segments left, stop. That matters because segmentCount goes up to 10. The common pitfall is hardcoding four nested loops, which breaks on other counts. Another is forgetting the leading-zero rule, so 01 slips through. Trying lengths 1 to 3 in ascending order gives lexicographic output for free, since shorter prefixes of digits sort first. Still, sorting at the end is a cheap safety check. If you blank mid-OA, StealthCoder can supply the recursion skeleton while you verify the edge cases.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Restore Addresses with K Segments 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. If you're reading this with an OA window open, you're who this was built for.
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 ByteDance's OA.
ByteDance reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Restore Addresses with K Segments FAQ
What's the trick in Restore Addresses with K Segments?+
Backtracking. Track the start index and segments remaining, try segment lengths 1 to 3, validate each one, and recurse. Accept a path only when segments remaining is 0 and every digit has been used. The parameterized segment count is the only change from the classic IPv4 version.
How hard is this ByteDance OA question really?+
Medium. The logic is short, but the validation rules are easy to get wrong. Leading zeros and the 255 cap cause most failed cases. If you know backtracking, you can write it in 15 minutes. Without that pattern, it feels harder than it is.
How do I get lexicographic order without extra work?+
Try segment lengths in ascending order, 1 then 2 then 3. Because dots are inserted into the same digit sequence, shorter first segments produce smaller strings at the dot position. You can also sort the result list at the end if you want a guaranteed ordering.
What edge cases should I test before submitting?+
Test digits with zeros like 010010 and 0000. Test segmentCount of 1, where the whole string must be a valid segment. Test strings too short or too long for the count, which should return an empty array. Also test segmentCount above 4, since hardcoded logic breaks there.
How do I prepare for this in 48 hours?+
Write the IPv4 restoration solution from scratch twice, then change it to take a segment count parameter. Add pruning on remaining length. Then do two or three other backtracking problems, such as generating parentheses or palindrome partitioning, so the recursion template feels automatic.