Insert Plus, Minus, and Times Between Digits to Hit a Target

Last updated: November 29, 2026

Quick Overview

Given a digit string and a target integer, insert binary operators +, -, or * between some digits (or none, so digits join into multi-digit numbers) and return every expression that evaluates to the target.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Given a digit string and a target integer, insert binary operators +, -, or * between some digits (or none, so digits join into multi-digit numbers) and return every expression that evaluates to the target.

A well-known hard Google onsite backtracking problem. The interviewer is checking whether you can handle multiplication precedence during the search by tracking the last operand, and whether you remember the leading-zero rule.

What the Interviewer Expects
  • Tracks running value and last operand to undo the previous term for *
  • Forbids multi-digit operands with a leading zero
  • Treats the first operand specially (no operator before it)
  • Gives the O(4^n) bound and explains the branching
  • Uses a list path or string slicing cleanly
Key Topics to Cover
Backtracking
Expression evaluation
Operator precedence
String building
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
  • How would you add division with integer truncation?
  • How would you add parentheses as an option?
  • How would you count expressions without building 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
  • Input: num (digits only, length up to about 10) and target (int).
  • Output: all expressions using every digit in order, for example "123", 6 gives 1+2+3 and 1*2*3.
  • Operands with leading ...
High-Level Design

Recurse over the start index of the next operand. At each step, choose an operand num[i..j] (rejecting a leading zero when longer than one digit), then choose an operator to place before it.

The tr...


Submit Your Answer
Markdown supported