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