Domanda di colloquio di Oracle

Question: Write pseudo code to validate and evaluate a given arithematic expression Example: a+(b*c)+(d/e) If the input is syntactically wrong, display an error message else display the result

Risposte di colloquio

Anonimo

5 ago 2017

The interviewer was probably looking for the particular answer only. With a stack to be used for parentheses validation and simultaneously evaluating the expression. You take 2 stacks, one for (operators,braces) and another for operands. In the operand stack, you push the values and in the operator stack, you push the operators and braces until you encounter a closing brace. Then you pop the last operator and pop two values from the operand stack, perform the operation and push the result back to value stack. Do this until you pop the opening brace from the operator stack. At the end, you will find the operand stack empty and value stack having one value, which is the result of the expression.

3

Anonimo

7 ago 2016

Me : Tried to evaluate based on parenthesis positions, slicing into substrings and all. But the interviewer reported that may not be approach is not adoptable to injection of new operators and also can be a buggy code.Also the correct approach proposed by him is to use a stack to validate and evaluate after that.

1