Insert Plus or Times Between Ordered Numbers to Reach a Target, Evaluated Left to Right
Company: Pinterest
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
You are given a list of nonnegative integers and a target. Decide whether you can place either `+` or `*` between every pair of adjacent numbers so that the resulting expression equals the target. The numbers must stay in their original order.
The expression is evaluated strictly from left to right, ignoring the usual operator precedence. For example, `2 + 3 * 4` evaluates as `(2 + 3) * 4 = 20`, not `14`.
### Function Signature
```python
def can_reach_target(nums: list[int], target: int) -> bool:
...
```
### Rules
- Exactly one operator, `+` or `*`, goes in each of the `len(nums) - 1` gaps. Numbers cannot be concatenated, reordered, skipped, or negated, and no parentheses can be added.
- Evaluation starts with `nums[0]` as the running value. For each later index `i`, the running value becomes `running + nums[i]` or `running * nums[i]`, depending on the operator in that gap.
- If `nums` has one element, there are no operators, and the answer is whether that element equals `target`.
- Return `True` if at least one assignment of operators produces exactly `target`. Otherwise, return `False`.
### Constraints
- `1 <= len(nums) <= 10`
- `0 <= nums[i] <= 30`
- `0 <= target <= 10^15`
- Intermediate and final values can exceed `2^31 - 1`, but they never exceed `30^10` (about `5.9 * 10^14`), so 64-bit integers are enough.
### Examples
Input: `nums = [2, 3, 4], target = 20`
Output: `True`
`(2 + 3) * 4 = 20`.
Input: `nums = [1, 2, 3], target = 7`
Output: `False`
The four possible assignments give `1 + 2 + 3 = 6`, `(1 + 2) * 3 = 9`, `1 * 2 + 3 = 5`, and `1 * 2 * 3 = 6`. With standard precedence, `1 + 2 * 3` would equal `7`, but that evaluation order is not used here.
Input: `nums = [5], target = 5`
Output: `True`
Overview: Decide whether placing a plus or times operator between every pair of adjacent numbers, kept in their original order, can produce a target value when the expression is evaluated strictly left to right. Tests careful handling of evaluation order, operator choices, and the size of the search space.
Read the full Pinterest Software Engineer interview experience this question came from
You are given a list of nonnegative integers `nums` and an integer `target`. Decide whether you can place either `+` or `*` between every pair of adjacent numbers so that the resulting expression equals `target`. The numbers must stay in their original order.
The expression is evaluated strictly from left to right, ignoring the usual operator precedence. For example, `2 + 3 * 4` evaluates as `(2 + 3) * 4 = 20`, not `14`.
Rules:
- Exactly one operator, `+` or `*`, goes in each of the `len(nums) - 1` gaps. Numbers cannot be concatenated, reordered, skipped, or negated, and no parentheses can be added.
- Evaluation starts with `nums[0]` as the running value. For each later index `i`, the running value becomes `running + nums[i]` or `running * nums[i]`, depending on the operator in that gap.
- If `nums` has one element, there are no operators, and the answer is whether that element equals `target`.
- Return `True` if at least one assignment of operators produces exactly `target`. Otherwise, return `False`.
The return value is a single boolean, so there is no ordering or tie-breaking choice to make.
Intermediate and final values can exceed `2^31 - 1` (they never exceed `30^10 = 590490000000000`), so use 64-bit integers: `long` in Java and `long long` in C++.
Example 1:
Input: `nums = [2, 3, 4], target = 20`
Output: `True`
Explanation: `(2 + 3) * 4 = 20`.
Example 2:
Input: `nums = [1, 2, 3], target = 7`
Output: `False`
Explanation: the four assignments give `1 + 2 + 3 = 6`, `(1 + 2) * 3 = 9`, `1 * 2 + 3 = 5`, and `1 * 2 * 3 = 6`. With standard precedence, `1 + 2 * 3` would equal `7`, but that evaluation order is not used here.
Example 3:
Input: `nums = [5], target = 5`
Output: `True`
Explanation: there are no operators, and the single element equals the target.
Constraints
- 1 <= len(nums) <= 10
- 0 <= nums[i] <= 30
- 0 <= target <= 10^15
- Exactly one operator, + or *, goes in each of the len(nums) - 1 gaps. Numbers cannot be concatenated, reordered, skipped, or negated, and no parentheses can be added.
- The expression is evaluated strictly from left to right, ignoring the usual operator precedence.
- Intermediate and final values can exceed 2^31 - 1, but they never exceed 30^10 (about 5.9 * 10^14), so 64-bit integers are enough (long in Java, long long in C++).
Examples
Input: ([5], 5)
Expected Output: True
Explanation: Minimum valid input: one element and no operators, and 5 equals the target.
Input: ([5], 7)
Expected Output: False
Explanation: Minimum valid input: the only reachable value is 5, which is not 7.
Hints
- There are only len(nums) - 1 gaps and at most 10 numbers, so the total number of distinct operator assignments is small; you do not need a clever formula.
- Because evaluation is strictly left to right, the only thing a prefix of the expression contributes to the rest of the computation is the single running value it produced.
- Do not assume the running value only grows: multiplying by 0 collapses it and multiplying by 1 leaves it unchanged, so discarding a state merely because it already passed the target would lose real answers.