r/csinterviewproblems • • Dec 18 '15

Evaluate Math Expression

As mentioned in title.

You're given a math expression in a string, return the result as an int.

Example: "10+2*3-5/2" -> 14.

Basic: four basic operations, +-*/ Bonus: parenthesis, power.

7 Upvotes

10 comments sorted by

View all comments

Show parent comment

1

u/pxdra Dec 20 '15

I've both been asked this question and used this question in interviews. I actually like it a lot, you can evaluate a bunch of things from it.

Having parenthesis doesn't actually make it much more complicated. An easy way is to enter a recursion and pop back out once you see a closing bracket.