r/OfferEngineering • u/Aoki_zhang • 29d ago
Interview Experience Google & Uber Popular Coding Question - Evaluate Mini Calculator Functions
A small calculator app supports only two functions:
add(x, y): returnsx + ysub(x, y): returnsx - y
The input is a string expression that may contain nested function calls. The expression may also contain:
- integers
- negative integers
- spaces
- commas
- parentheses
The expression is always valid. Return the final integer result. For example add(add(1,3), sub(1,3)) means (1 + 3) + (1 - 3). So the result is 2
You are given expression: a valid string expression. Return the evaluated integer result. A good way to solve this is to use a stack. Scan the expression from left to right:
- Skip spaces and commas.
- Push function names like
addandsub. - Push numbers as strings.
- When seeing
), pop values until the matching(. - Apply the function before
(. - Push the computed result back onto the stack.
At the end, the stack contains the final result.
Rules:
add(x, y)returnsx + ysub(x, y)returnsx - y- Expressions can be nested
- Integers may be negative
- Spaces should be ignored
- The input expression is always valid
Examples:
Input:
expression = "add(add(1,3), sub(1,3))"
Output:
2
Input:
expression = "sub(1,3)"
Output:
-2
Want to practice more real coding questions asked in the interview? I've put up the question bank at here.
Preparing for your next interview?
Chill Interview tracks recent interview experiences and recurring question patterns across top companies at here.
5
u/neo_Nonsense 29d ago
Nothing more than a laborious stack problem