Skip to content

Commit e529c57

Browse files
author
Joseph Luce
authored
Update compute_string.md
1 parent fd42ca1 commit e529c57

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

real_interview_questions/Google/compute_string.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
# QUESTION
2+
Given a string containing numbers and operators (+ and \*), calculate the result. The string can contain brackets (\[ and \]) which means you must perform that operation before adding any other operators to it.
3+
4+
For example, '1+2-\[3-4\]' results in 4.
5+
16
# SOLUTION (Run: O(n), Space: O(n), N = number of characters in string)
27
This was one given during an onsite interview at google. I did this question with recursion which I believe was a mistake.
38
Before the interview, he said he was testing my ability to write in Python, but the recursion method wasn't a good way to show that.
49
Now this solution is the dirty way to do, there is a cleaner pythonic method to this down below.
510

6-
Overall, this is a medium question, as long as you can see that stacks need to be used in one way or the other, the question is fairly straight forward.
11+
Overall, this is a medium question, as long as you can see that stacks need to be used in one way or the other, the question is fairly straight forward. By using two stacks, one to keep the numbers and one to keep the operators in, we can easily retrieve the past calculated answers and perform operations on them.
712

813
```
914
def calculate_string(input):
@@ -39,7 +44,8 @@ def get_result(n1, n2, operator):
3944
elif operator == '-':
4045
return n1 - n2
4146
42-
assert calculate_string('1+2-[3+4]') == -4
47+
assert calculate_string('1+2-[3-4]') == 4
48+
#assert calculate_string('1+2-[-3-4]') == 10
4349
assert calculate_string('[1-2]+[3+4]') == 6
4450
assert calculate_string('1+2-3-4') == -4
4551
assert calculate_string('[1-2]+3-4') == -2

0 commit comments

Comments
 (0)