Amazon Interview Question
Software Engineer / Developers<pre lang="" line="1" title="CodeMonkey35160" class="run-this">// supose an expression e can be:
// e = variable
// |= constant
// |= e comparator e
InfixExpression PostInConvert (PostfixExpression pe) {
if (isConstant(pe) || isVariable(pe)) return pe;
InfixExpression ie = new InfixExpression();
ie.comparator = pe.comparator;
ie.left = PostInConvert(pe.first);
ie.right = PostInConvert(pe.second);
return ie;
}</pre><pre title="CodeMonkey35160" input="yes">
</pre>
<pre lang="" line="1" title="CodeMonkey1135" class="run-this">// here is the parsing code from String to PostfixExpression
// well.... it is just a pseudo code
PostfixExpression parser (String inputs) {
Stack<String> stack_main = new Stack();
Stack<Expression> stack_keep = new Stack();
while (inputs.hasNextToken()) {
stack_main.push(inputs.nextToken());
}
while (stack_main.empty() == false) {
if (isComparator(stack_main.peek())) {
PostfixExpression newPE =
new PostfixExpression(stack_main.pop(),
stack_keep.pop(),
stack_keep.pop());
stack_keep.push(newPE);
}
else {
stack_keep.push(newPostfixExpression(stack_main.pop()));
}
}
return stack_keep.pop();
}</pre><pre title="CodeMonkey1135" input="yes">
</pre>
- Kunal May 22, 2011