Expression Add Operators
Reported by candidates from Pinterest's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Pinterest reported this one in August 2026, and the input size is the whole story. Ten digits means 9 gaps, each with four choices (+, -, *, or nothing), so roughly 4^9 expressions. That's about 262k, small enough to enumerate but only if you evaluate each one smartly. This is Expression Add Operators, a backtracking problem with a multiplication-precedence trap. If you blank on how to track the last operand, StealthCoder is the silent safety net on the live OA. Know the shape before you sit down: DFS over split points, running value, and the previous term.
The problem
Given a string digits containing only decimal digits and an integer target, insert the binary operators +, -, or * between some adjacent digits so that the resulting arithmetic expression evaluates to target. You may concatenate adjacent digits to form a multi-digit operand. An operand may be 0, but a multi-digit operand must not start with 0. Use ordinary multiplication precedence: multiplication is evaluated before addition and subtraction. Every digit must appear exactly once and in its original order. Return every valid expression in ascending lexicographic order. Do not include duplicate expressions. Function addOperators(digits: String, target: int) → String[] Examples Example 1 digits = "123" target = 6 return = ["1*2*3","1+2+3"] Both 1 * 2 * 3 and 1 + 2 + 3 evaluate to 6. The returned strings are sorted lexicographically. Example 2 digits = "105" target = 5 return = ["1*0+5","10-5"] The valid expressions are 1 * 0 + 5 and 10 - 5. An expression such as 1 * 05 is invalid because 05 has a leading zero. Example 3 digits = "3456237490" target = 9191 return = [] No permitted placement of the three operators produces the target. Constraints 1 <= digits.length <= 10 digits contains only characters from 0 through 9. -2^31 <= target <= 2^31 - 1 Intermediate arithmetic fits in a signed 64-bit integer for all explored operands and expression values under these bounds.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is backtracking with three state variables: current index, running total, and the last multiplied term. When you add a new operand n with *, you can't just multiply the total. You undo the last term and redo it: total - last + last * n, and the new last becomes last * n. For + and -, last becomes n or -n. The common pitfalls are leading zeros (if the operand starts with 0 and has length over 1, break the loop), overflow (use 64-bit), and the first operand, which has no operator in front of it. Sorting is the last gotcha. Either sort the final list or generate in an order that already guarantees it. Sorting at the end is safest, and with the output this small it costs nothing. StealthCoder is the hedge if the precedence bookkeeping slips on the live OA.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Expression Add Operators 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 by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as expression add operators. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Pinterest's OA.
Pinterest reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Expression Add Operators FAQ
What's the trick in Expression Add Operators?+
Track the last term separately from the running total. For multiplication, subtract the last term from the total, then add last times the new operand. That handles precedence without building and parsing a string. Everything else is plain DFS over where to cut the digits.
How hard is this really for the Pinterest OA?+
It's a hard-tagged backtracking problem, but the input is capped at 10 digits, so no clever pruning is required. The difficulty is getting precedence and leading zeros right on the first try. If you've seen the last-term trick, it's a 20 minute job.
What's the time complexity?+
Roughly O(4^n) times n for building strings. With n up to 10 that's a few hundred thousand paths, which is fine. Brute force is the intended approach here. You don't need memoization, because the state includes the running value and last term.
How do I handle leading zeros?+
While extending the operand across digits, if the first character of the slice is 0 and you've already taken more than one digit, stop. A single 0 is allowed as an operand, so 1*0+5 is valid, but 05 is not. Check this inside the loop, not after.
How do I get the output in lexicographic order with no duplicates?+
Each expression comes from a unique sequence of cuts and operators, so duplicates don't arise in a clean DFS. Collect results in a list and sort it at the end. The result set is small, so sorting costs almost nothing.