Equal-Sum Digit Partitions
Reported by candidates from Alchemy's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Alchemy reported this one in May 2022, and the input cap is the first thing to read. Sixteen digits means 15 gaps, so 2^15 = 32,768 possible cuts. That's small enough to brute force, and the real question is how cleanly you do it. The task is to return every way to cut the digit string into segments that all share the same digit sum. The uncut string always counts. If the OA invite is sitting in your inbox, this is a math-flavored enumeration problem, not a hard one. StealthCoder is there as a safety net on the live OA if you blank on the details.
The problem
Given a nonempty string digits containing decimal digits, insert zero or more underscores between adjacent digits. Keep a partition exactly when every contiguous segment has the same digit sum. Return all valid partition strings in standard lexicographic order. The uncut input is always a valid one-segment partition. Leading zeroes remain part of their original segments. Function equalSumPartitions(digits: String) → String[] Examples Example 1 digits = "1741380" return = ["1741380","174_1380","17_413_80"] The three valid cuts have common segment sums 24, 12, and 8 respectively. Example 2 digits = "1111" return = ["1111","11_11","1_1_1_1"] Constraints 1 <= digits.length <= 16. digits contains only characters 0 through 9.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: any valid partition's common segment sum must divide the total digit sum. So don't enumerate all 32,768 masks blindly. Compute the total, then for each target sum that divides it, walk the string greedily. Add digits to the running segment, cut exactly when the running sum hits the target, and fail if it overshoots. Because digits are nonnegative, greedy works for positive targets. The pitfall is zeros. A zero adds nothing to the sum, so the cut can land in more than one place, and greedy alone misses those. A backtracking walk handles it: at each position, cut when the sum equals the target and also try continuing through zeros. If the total is 0, every cut is valid, so return all 2^(n-1) strings. Sort the results lexicographically at the end. Watch that '_' sorts after digits in ASCII. If you blank on the zero cases, StealthCoder can give you a working solution live on the OA.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Equal-Sum Digit Partitions 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Alchemy's OA.
Alchemy reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Equal-Sum Digit Partitions FAQ
How hard is Equal-Sum Digit Partitions really?+
Easy to medium. The 16-digit cap means brute force over all 2^15 cut masks passes. The difficulty is edge cases: all-zero input, zeros inside segments, and sorting the output correctly. Get those right and the rest is straightforward.
What's the trick to avoid wasted work?+
The common segment sum must divide the total digit sum. Compute the total once, then only try targets that divide it. Within a target, backtrack and prune as soon as a running segment sum exceeds the target. That cuts the search a lot.
How should I handle zeros in the digits?+
Zeros add nothing to a segment sum, so a cut can sometimes go before or after them and both are valid. Pure greedy misses that. Use backtracking that tries both options when the running sum equals the target and the next digit is 0. If the total is 0, every partition is valid.
How do I get the output order right?+
Sort at the end with a standard string sort. Underscore has a higher ASCII value than any digit, so '1111' comes before '11_11', which comes before '1_1_1_1'? Check Example 2: the expected order matches that of a plain sort of the strings, so just sort and don't hand-order.
How do I prepare for this in 48 hours?+
Write the mask-based brute force first, since it's short and correct at this size. Then add the divisibility prune. Test on '1111', '1741380', and '0000'. Make sure the uncut string is always present in the output.