Different Ways To Add Parenthesis
Suppose you have a string 'S' representing an arithmetic expression. Your task is to compute all potential outcomes by adding valid parentheses in different ways to evaluate the expression.
Example:
Input:
S = "2 * 3 - 2"
Output:
[4, 2] or [2, 4]
Explanation:
For the expression S = 2 * 3 - 2
, the two valid computations are:
1. ((2 * 3) - 2) = 4
2. (2 * (3 - 2)) = 2
Input:
The first line contains an integer 'T', the number of test cases.
The next 'T' lines each contain an arithmetic expression with numbers and operators '+', '-', '*'.
Output:
For each test case, output all the outcomes from the expression after adding valid parentheses.
Results can be in any order, and each test case's results should be printed on a separate line.
Constraints:
- 1 <= T <= 10
- 3 <= len(S) <= 65
- Each input expression 'S' must contain at least one operator.
- Time limit: 1 second.
Note:
No need to print anything explicitly; focus on implementing the provided function.

AnswerBot
4mo
Compute all potential outcomes by adding valid parentheses in different ways to evaluate an arithmetic expression.
Iterate through the expression and try adding parentheses at different positions to ev...read more
Help your peers!
Add answer anonymously...
GoMechanic Software Developer Intern interview questions & answers
A Software Developer Intern was asked Q. Different Ways To Add Parenthesis Suppose you have a string 'S' representing an ...read more
A Software Developer Intern was asked Q. Time to Burn Tree Problem You are given a binary tree consisting of 'N' unique n...read more
A Software Developer Intern was asked Q. Delete the Middle Node from a Singly Linked List Given a singly linked list of i...read more
Popular interview questions of Software Developer Intern
A Software Developer Intern was asked Q1. Different Ways To Add Parenthesis Suppose you have a string 'S' representing an ...read more
A Software Developer Intern was asked Q2. Time to Burn Tree Problem You are given a binary tree consisting of 'N' unique n...read more
A Software Developer Intern was asked Q3. Delete the Middle Node from a Singly Linked List Given a singly linked list of i...read more
>
GoMechanic Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

