Minimize Expression with Parentheses
Updated
Problem statement
You are given a string expression in the form num1+num2, where num1 and num2 are positive integers.
Insert exactly one left parenthesis somewhere to the left of the plus sign and exactly one right parenthesis somewhere to the right of the plus sign. Digits immediately outside the parentheses are multiplied by the value inside them. A missing factor on either side is treated as 1.
Return any valid parenthesized expression whose numeric value is as small as possible. If several placements produce the same minimum value, any of them is valid.
Function
minimizeExpressionWithParentheses(expression: String) → StringExamples
Example 1
expression = "247+38"return = "2(47+38)"The placement 2(47+38) evaluates to 2 × 85 = 170, which is the minimum over all valid placements.
Example 2
expression = "12+34"return = "1(2+3)4"The placement 1(2+3)4 evaluates to 1 × 5 × 4 = 20, the smallest possible value.
Example 3
expression = "999+999"return = "(999+999)"Putting both full numbers inside the parentheses gives 999 + 999 = 1998. Every placement with an outside factor is larger.
Constraints
3 <= expression.length <= 10expressioncontains digits from'1'through'9'and exactly one plus sign'+'.expressionstarts and ends with a digit.- Every value produced by a valid parenthesis placement fits in a signed
32-bit integer.
More Pinterest interview questions
- Gaussian Naive Bayes Classifier (Machine Learning OA)OA · Seen Nov 2026
- Bootstrap Decision Tree ClassifierOA · Seen Nov 2026
- Shortest Pin Path Through TopicsPHONE SCREEN · Seen Nov 2026
- Top K Pins with Insert and QueryPHONE SCREEN · Seen Nov 2026
- Pins Violation Log QueriesPHONE SCREEN · Seen Nov 2026
- Sparse Matrix Addition and MultiplicationPHONE SCREEN · Seen Nov 2026
- Minimum Bus Routes to a DestinationPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Delete and Compact a Forest SubtreePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Filter Pins by Excluded IDsONSITE INTERVIEW · Seen Oct 2026
- Top-Scoring Items in a Time WindowONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Binary CircuitAkuna Capital · OAAkuna Capital interview questions
- Minimize Difference Between Two NumbersMicrosoft · OAMicrosoft interview questions
- Longest Happy String with a Stable Tie RuleAdobe · OAAdobe interview questions
- Return Exact Cash Register ChangeBank of America · OABank of America interview questions
- Digit Changes for a Multiple of 9Nutanix · OANutanix interview questions
- Minimum Digit Swaps for the Smallest DifferenceLSEG · OALSEG interview questions
- Reorderable Task Scheduler With CooldownWaymo · PHONE SCREENWaymo interview questions
- Smallest Number With a Given Digit SumAmazon · ONSITE INTERVIEW
- Smallest and Largest Numbers with a Digit ProductJane Street · PHONE SCREENJane Street interview questions
- IP Range to CIDR BlocksAirbnb · PHONE SCREENAirbnb interview questions