diff --git a/interview-process/engineers/questions/parenthesis.md b/interview-process/engineers/questions/parenthesis.md index b9e8252..53483eb 100644 --- a/interview-process/engineers/questions/parenthesis.md +++ b/interview-process/engineers/questions/parenthesis.md @@ -13,13 +13,45 @@ Write a method that for a given input string, reverse all the characters inside ``` ## Observations -- How to solve it? Using a stack and a queue. +- How to solve it? Two possible solutions for this problem are using recursion or using a stack. + +## Possible Solutions +```python +def reverse_text_inside_parens(text): + pending_outputs = [] + output_so_far = '' + + for i in range(len(text)): + if text[i] == '(': + pending_outputs.append(output_so_far) + output_so_far = '' + + elif text[i] == ')': + if len(pending_outputs) == 0: + raise ValueError('Malformed expression') + + previous_output = pending_outputs.pop() + output_so_far = previous_output + output_so_far[::-1] + else: + output_so_far += text[i] + + if len(pending_outputs) > 0: + raise ValueError('Malformed expression') + + return output_so_far +``` ## Test cases ```javascript -foobarbaz => foobarbaz -foo(bar)baz => foorabbaz -foo(bar(baz))blim => foo(barzab)blim => foobazrabblim +'' => '' +'()' => '' +'(())(((())))' => '' +'(bar)' => 'rab' +'((bar))' => 'bar' +'wi(ez)(((il)))(en)' => 'wizeline' +'foobarbaz' => 'foobarbaz' +'foo(bar)baz' => 'foorabbaz' +'foo(bar(baz))blim' => 'foo(barzab)blim' => 'foobazrabblim' ``` [Home](../../../README.md) |