This question was asked to one of my friend. Not sure which company
Question :
Evaluate an expression containing parentheses following below rule
( ) - This evalutes to 1
( ) ( ) - This evaluates to 1 + 1 = 2
But if there is a parentheses enclosing another sets of parentheses it indicates multiply by 2.
for eg :
( ( ) ) - this becomes 2 * 1 = 2 ( inner ( ) evaluates to 1 and since it is inside another set of ( ) answer is 2 * 1)
( ( ) ( ) ) - This evaluates to 2 * ( 1 + 1) = 4
( ( ) ( ) ) ( ) - This evaluates to 2 * ( 1 + 1) + 1 = 5
Conditions :
We can assume that the expression is always valid.
The input will contain only ( and )
Can you please share, the logic for solving the problem?