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
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

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
Backtracking
Recursion
Strings
Operator precedence
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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...


Submit Your Answer
Markdown supported