Insert Plus, Minus, and Times Between Digits to Hit a Target
Last updated: November 29, 2026
Quick Overview
Given a digit string and a target integer, insert binary operators +, -, or * between some digits (or none, so digits join into multi-digit numbers) and return every expression that evaluates to the target.
0
0
0 solved
Given a digit string and a target integer, insert binary operators +, -, or * between some digits (or none, so digits join into multi-digit numbers) and return every expression that evaluates to the target.
A well-known hard Google onsite backtracking problem. The interviewer is checking whether you can handle multiplication precedence during the search by tracking the last operand, and whether you remember the leading-zero rule.
What the Interviewer Expects
- Tracks running value and last operand to undo the previous term for *
- Forbids multi-digit operands with a leading zero
- Treats the first operand specially (no operator before it)
- Gives the O(4^n) bound and explains the branching
- Uses a list path or string slicing cleanly
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you add division with integer truncation?
- How would you add parentheses as an option?
- How would you count expressions without building strings?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input:
num(digits only, length up to about 10) andtarget(int). - Output: all expressions using every digit in order, for example
"123", 6 gives1+2+3and1*2*3. - Operands with leading ...
High-Level Design
Recurse over the start index of the next operand. At each step, choose an operand num[i..j] (rejecting a leading zero when longer than one digit), then choose an operator to place before it.
The tr...