Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




Comment hidden because of low score. Click to expand.
0
of 0 vote

Algorithm

1. Read the characters
a. Opening brackets : push in the stack and go to step 1
b. operator: push in the stack and go to step 1
c. number: print
d. closing brackets: pop from stack
i. if it is a closing bracket - ignore
ii. if it is an operator - print
e. new line - exit

- pratikrd November 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

split the prefix into tokens and process those tokens one by one
use a stack
read a operator, push the token to the stack
read a non-operator, if the top element is operator, push the token to the stack otherwise, pop an element, which is the left operand, and another element, which is the operator, to construct an postfix expression, repeat it until the stack becomes empty or the top element is operator and then push the expression to the stack
pop the postfix expression and return it

public String prefixToPostfix(String prefix)
        {
                Stack<String> s = new Stack<String>();
                String[] tokens = prefix.split(" ");
                for(String token: tokens) {
                        if(isOperator(token)) {
                                s.push(token);
                        } else {
                                String operand = token;
                                while(!s.empty() && !isOperator(s.peek())) {
                                        operand = s.pop() + " " + operand + " " + s.pop();
                                }
                                s.push(operand);
                        }
                }
                return s.pop();
        }
        
        private boolean isOperator(String s)
        {
                return s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/");
        }

- dgbfs November 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Do you think we have to push the opening brackets. How will the post fix expression has the required brackets in proper order?

- Anjana November 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Question: If we are only given prefix, how are we to guess any structure to the tree. There can be multiple trees that give the same prefix.
Unless I am mistaken, we can only reconstruct a tree if we are given atleast two out of the tree recursive traversal techiques.

- Arunav Borthakur November 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

And also in that two orders one should be inorder traversal

- bhargav December 23, 2012 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More