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 the digits (or leaving them concatenated) that evaluates to the target. Operands may not have leading zeros.
Meta
November 29, 20260
0
0 solved
Given a string of digits and a target integer, return every expression formed by inserting +, -, or * between the digits (or leaving them concatenated) that evaluates to the target. Operands may not have leading zeros.
Expression add operators is a Meta onsite Hard that tests backtracking with precedence handled on the fly. The interviewer checks the trick of tracking the last multiplicative term so * can be undone, plus pruning numbers with leading zeros.
What the Interviewer Expects
- Backtracks over split points and operators
- Tracks running value and last operand to handle * precedence
- Rejects multi-digit operands starting with '0'
- Builds the expression string efficiently
- States the exponential bound honestly
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
- Add division with integer truncation.
- Return only the count of valid expressions; can you memoize?
- How would you parallelize the search for very long digit 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
Digit string length up to 10, target fits in a 32-bit int, intermediate values can exceed it (Python handles that). Return expressions in any order. The first operand has no sign before it....
High-Level Design
Recurse with (pos, expr, value, last), where value is the evaluated result so far and last is the signed value of the last multiplicative term. Choosing the next operand cur = int(s[pos:k]): f...