Minimize Result by Adding Parentheses to Expression
Question Details
You are given a string expression representing an expression made of decimal digits and exactly one plus sign +, e.g. "247+38".
Insert one pair of parentheses () into the expressi
Full Details
You are given a string expression representing an expression made of decimal digits and exactly one plus sign +, e.g. "247+38".
Insert one pair of parentheses () into the expression such that the parentheses must include the + and also include at least one digit on each side of the +.
After insertion:
- The content inside parentheses is evaluated as addition.
- Any digits left outside the parentheses are multiplied with the parenthesized result (equivalent to inserting * between the outside digits and the parenthesized value).
Return the resulting expression string (with parentheses) that yields the minimum possible value.
Note: The original post mentions there is no digit 0 in the string.
I/O
-
Input: one line string expression
Output: one line string of the minimized expression with parentheses
Constraints (OA-friendly)
- 3 <= len(expression) <= 50
- expression contains only '1'~'9' and '+'
- exactly one '+'
Examples
Input:
247+38
Output:
2(47+38)
Input:
12+34
Output:
(12+3)4
Sample Input
247+38
Sample Output
2(47+38)
Test Cases
Case 1
Input:
247+38
Output:
2(47+38)
Case 2
Input:
12+34
Output:
1(2+3)4
Case 3
Input:
999+1
Output:
(999+1)
Case 4
Input:
1+999
Output:
(1+999)
Case 5
Input:
111+111
Output:
1(11+111)
About This Question
This is a reported interview question from a pinterest interview for a swe role during the coding round.
It covers the following topics: Strings .
Topics
More Pinterest Interview Questions
About Pinterest Interview Reports
This question was reported by a candidate who interviewed at Pinterest. LeakCode aggregates interview reports from 10+ sources, including 1Point3Acres, Glassdoor, LeetCode Discuss, Blind, Reddit, Indeed, and Nowcoder. Each report is translated where necessary, deduplicated against existing entries, and tagged by company, role, round type, and reporting date.
Use this question as one calibration data point, not a memorization target. Companies typically rotate their question pools every 2-4 months; the exact wording of a 2024 question may differ from what you encounter today. The underlying pattern, difficulty level, and follow-up depth at Pinterest are the higher-signal extractions to take from this report.
For broader preparation context, the Pinterest interview process typically includes a recruiter screen, one or two technical phone screens, and a 4-5 round on-site loop covering coding, system design (at L4+ levels), and behavioral. Reports tagged on LeakCode show the round-by-round distribution and typical difficulty calibration. To browse questions filtered by round type and seniority, use the company hub linked above.
How To Practice This Type of Question
Solve similar problems on LeetCode under timed conditions (25-35 minutes per medium difficulty). The goal is pattern recognition: recognize the underlying technique (sliding window, two-pointer, BFS, memoized recursion, etc.) within 60-90 seconds of reading. Strong candidates verbalize their hypothesis out loud before coding, then iterate based on feedback. Weak candidates dive into implementation immediately, lose time on the wrong approach, and run out of time for follow-ups.
Companies update their question pools every 2-4 months. The exact wording of any given question may have been retired by the time you interview. Focus your prep on the pattern, not the specific problem. The patterns that appear in Pinterest reports consistently are the ones worth investing in; one-off niche problems are not.
During Your Pinterest Round
Apply the standard interview round template: clarify requirements (2-3 minutes), state your approach out loud and confirm direction with the interviewer (3-5 minutes), code with narration (15-25 minutes), test with concrete examples including edge cases (5 minutes), discuss optimization or trade-offs if time permits (5 minutes). This template is universally accepted across FAANG and adjacent companies; deviating from it produces weaker interviewer feedback signal.
The single most predictive failure mode in Pinterest reports tagged "no hire": not asking clarifying questions. Interviewers are explicitly trained to weight this. Strong candidates ask 3-5 clarifying questions even on problems that look obvious; weak candidates dive into code immediately. The clarifying-question check is often the first signal recorded in the interviewer's written notes.