← 返回 waymo 的题目列表Build an Arithmetic Expression to Reach a Target
类型:qbank
Given three numbers and a target, insert addition, subtraction, multiplication, and parentheses between the numbers; return an expression whose value equals the target, or determine that none exists. The follow-up generalizes the input from three numbers to n numbers.
Requirements
Input: three numbers and a target value.
Insert +, -, *, and parentheses between the supplied numbers in any valid arrangement.
Return an expression that evaluates to the target when one exists; otherwise indicate that no expression works.
Follow-up: generalize the procedure from three numbers to an arbitrary count n.
Notes
Clarify whether every number must be used exactly once, whether their original order must be preserved, and how duplicate values should be distinguished before coding.
Clarify the numeric domain and equality rule: integers only versus non-integers, and exact comparison versus a floating-point tolerance.
The n-number follow-up materially changes the search size; state the complexity of the proposed enumeration and discuss safe ways to avoid repeating equivalent states.
Preparation
Implement an expression builder that carries both each intermediate numeric value and the corresponding expression string.
Drill the n-number extension with duplicate inputs, negative intermediate values, and unreachable targets.
Practice stating the input-use, ordering, and numeric-precision assumptions in the first two minutes.