Insert Addition and Multiplication Operators
Updated
Problem statement
You are given an integer array numbers and an integer target. Insert exactly one binary operator, either + or *, between every pair of adjacent array elements.
Evaluate each resulting expression using ordinary arithmetic precedence, so every multiplication is performed before addition. Return every expression whose value equals target.
- Use every number exactly once and preserve the original order.
- Do not concatenate adjacent numbers.
- Write each number in its ordinary decimal form with no spaces around operators.
- Return distinct expressions in ascending lexicographic order.
Function
findExpressions(numbers: int[], target: long) → String[]Examples
Example 1
numbers = [1,2,3,4]target = 10return = ["1*2*3+4","1+2+3+4"]Under normal precedence, both 1 * 2 * 3 + 4 and 1 + 2 + 3 + 4 equal 10. No other placement of the permitted operators does.
Example 2
numbers = [2,3,2]target = 8return = ["2*3+2","2+3*2"]Multiplication is evaluated first, so both 2 * 3 + 2 and 2 + 3 * 2 equal 8.
Example 3
numbers = [0,5,0]target = 0return = ["0*5*0","0*5+0","0+5*0"]Zero-valued multiplication groups create three different valid expressions. The result is sorted lexicographically.
Constraints
2 <= numbers.length <= 15.0 <= numbers[i] <= 9.0 <= target <= 10^15.- Every complete expression value fits in a signed 64-bit integer under these bounds.
More Pinterest Onsite Interview questions
- Minimum Bus Routes to a DestinationPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Delete and Compact a Forest SubtreePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Filter Pins by Excluded IDsONSITE INTERVIEW · Seen Oct 2026
- Top-Scoring Items in a Time WindowONSITE INTERVIEW · Seen Oct 2026
- Optimal Account BalancingPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Escape Room LeaderboardPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Policy Violation Set ChecksONSITE INTERVIEW · Seen Sep 2026
- Shortest Sequence of Source SubsequencesONSITE INTERVIEW · Seen Sep 2026
- Unique Active Pins by Time SegmentONSITE INTERVIEW · Seen Sep 2026
- Coin ChangeONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Maximum Unique-Character Word SubsetMeta · ONSITE INTERVIEWMeta interview questions
- Word Break IIAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Word Break IIBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Word Search IIKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Largest Number Below N from Reusable DigitsByteDance · PHONE SCREENByteDance interview questions
- Generate All Permutations of a NumberFleetx · ONSITE INTERVIEW
- Rank Word-Translation Hints from Sentence TranslationsDuolingo · PHONE SCREEN · ONSITE INTERVIEWDuolingo interview questions
- Group AnagramsAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Response Time Stats by URL PatternGusto · ONSITE INTERVIEWGusto interview questions