Insert Operators Between Price Digits to Reach a Target Total
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 a multi-digit number) that evaluates to the target. Operands may not have leading zeros.
Amazon
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 a multi-digit number) that evaluates to the target. Operands may not have leading zeros.
A hard onsite backtracking question in Amazon's style. The core challenge is evaluating with correct multiplication precedence during the search, without re-parsing each candidate, by carrying the last multiplicative term.
What the Interviewer Expects
- Enumerates operand splits and operators via DFS
- Tracks running value and last operand to handle * precedence
- Rejects multi-digit operands with a leading zero
- Handles the first operand without a leading operator
- Analyzes O(4^n) growth
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 return only the count of expressions?
- How would you memoize, and why does it help little here?
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 1 to 10, target in 32-bit range. Return all expressions (sorted here) whose value equals the target, using standard precedence (* before + and -). An operand like 05 is...
High-Level Design
DFS over the position i: choose the next operand digits[i:j], and for every position after the first, choose an operator. Evaluate incrementally with two numbers: value (the expression so far) a...