Insert Plus, Minus and Times Between Digits to Hit a Target
Last updated: November 29, 2026
Quick Overview
Given a string of digits and a target integer, return every expression formed by inserting +, - or * between digits (or nothing, to join digits into multi-digit numbers) that evaluates to the target, with no operand having a leading zero.
Microsoft
November 29, 20260
0
0 solved
Given a string of digits and a target integer, return every expression formed by inserting +, - or * between digits (or nothing, to join digits into multi-digit numbers) that evaluates to the target, with no operand having a leading zero.
A hard Microsoft onsite backtracking problem that tests whether a candidate can evaluate an expression while it is being built, without re-parsing it at every leaf. The trick for multiplication precedence, tracking the last operand, is exactly what the interviewer listens for.
What the Interviewer Expects
- Backtrack over operand lengths and operator choices
- Evaluate incrementally with a running value and last operand
- Undo the last operand to apply multiplication precedence
- Reject operands with leading zeros
- Analyze the 4^n style search space
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 exact results only?
- How would you count solutions without building strings?
- How would you prune when the target is far from any reachable value?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Digits string of length up to 10, target that may be large or negative. Between each pair of adjacent digits we may put +, -, *, or nothing. Operands like 05 are not allowed, but 0 alone is....
High-Level Design
Backtrack on the next operand: from index i, choose its end j, then choose the operator in front of it. Building the full string and calling an evaluator at every leaf would cost O(n) per leaf; in...