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

Last updated: November 29, 2026

Quick Overview

Given a digit string and a target integer, return every expression formed by inserting +, - or * between digits (multi-digit operands allowed, no leading zeros) that evaluates to the target with normal precedence.

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

0

0

0 solved


Given a digit string and a target integer, return every expression formed by inserting +, - or * between digits (multi-digit operands allowed, no leading zeros) that evaluates to the target with normal precedence.

This is a classic ByteDance onsite hard problem in the style of the bar-raiser coding round. The interviewer is checking whether you can handle operator precedence during backtracking without building and evaluating strings at the leaves, which is the difference between a passing and a timing-out solution.

What the Interviewer Expects
  • Enumerate operand splits and operators with backtracking
  • Track the last multiplicative term to undo it when a * follows
  • Block operands with leading zeros
  • Avoid evaluating each full expression with eval
  • State the exponential bound honestly
Key Topics to Cover
backtracking
operator precedence
expression evaluation
string enumeration
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 count expressions instead of listing them, and can you memoize?
  • What breaks if the input can be 30 digits long?
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 with up to 10 digits and an integer target. Output: all valid expression strings, in any order. Operands may be multi-digit but not start with 0 unless they are exactly 0. Unary opera...

High-Level Design

DFS over the index i with three running values: the expression so far, its total value, and last, the value of the most recent term in a product chain. Adding +x sets value to value + x and `l...


Submit Your Answer
Markdown supported