The Expression Add Operators interview question asks you to find all possible strings formed by inserting binary operators (+, -, or *) between the digits of a string num such that the expression evaluates to a target value. You cannot change the order of digits, but you can concatenate them to form multi-digit numbers.
Companies like Meta, Amazon, and Google ask this Hard difficulty Backtracking problem to test a candidate's mastery of recursion and mathematical evaluation. It requires careful handling of operator precedence (especially multiplication) and leading zeros. It's an excellent test of a candidate's ability to maintain complex state (the current expression, current value, and the "last operand" for multiplication) through a recursive search.
This problem is solved using Backtracking with a "prev_operand" state to handle multiplication precedence.
current_val and prev_val through the recursion.a + b * c, you cannot simply do (a + b) * c. Instead, you subtract b from the running total and add b * c.
new_val = current_val - prev_val + (prev_val * next_num)num = "123", target = 6
[1, 2, 3] with [+, -, *, ""].long) for the evaluation, as intermediate products can exceed 32-bit limits.Practice the multiplication trick: total - prev + (prev * current). This is a very common requirement for any problem where you need to evaluate math expressions on the fly without a full parser.