Amazon Interview Question for Software Engineer / Developers






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

namespace InfixToPostFix
{
    class Program
    {
        public  static Hashtable precedence = new Hashtable();
        public static string sOutPutString;
         
        
        static void Main(string[] args)
        {
            string input = "a+b*c-d";

            precedence.Add("/", 4);
            precedence.Add("*", 3);
            precedence.Add("+", 2);
            precedence.Add("-", 1);

            foreach(DictionaryEntry Entry in precedence)
            {
              Console.WriteLine("{0} and {1}",Entry.Key, Entry.Value);
            }
           
            inFixtoPost(input);
            Console.Write(sOutPutString);
            Console.Read();
        }

        public static void inFixtoPost(string infix)
        {
            
            Stack s = new Stack();
            char[] cArrinput = infix.ToCharArray();
            Boolean bIsOperand;

           for (int i = 0; i < cArrinput.Length; i++)
           {
               char cTemp = cArrinput[i];
               bIsOperand = isOperand(cTemp);
              

               if (bIsOperand == true)
               {
                                     

                   if (s.Count == 0) // If stack is empty
                   {
                       s.Push(Convert.ToChar(cTemp));
                   }
                   else // If stack is not empty
                   {
                       int PeekPrecedence = Convert.ToInt32(precedence[s.Peek().ToString()]);
                       int iCurrentPrecedence = Convert.ToInt32(precedence[cTemp.ToString()]);
                       while (iCurrentPrecedence < PeekPrecedence && s.Count >0)
                       {
                           Char sTemp = Convert.ToChar(s.Pop());
                           sOutPutString = sOutPutString + sTemp;
                       }

                       s.Push(cTemp);                       
                   }
               }
               else // if it is not an operand               
               {
                   sOutPutString = sOutPutString + cTemp;
               }

               if (i == cArrinput.Length - 1)
               {
                   while (s.Count > 0)
                   {
                       sOutPutString = sOutPutString + Convert.ToChar(s.Pop());
                   }
               }
           }          
         
           
        }

        public static Boolean isOperand(char cOperandCheck)
        {
            if (cOperandCheck == '+' || cOperandCheck == '/' || cOperandCheck == '-' || cOperandCheck == '*')
            {
                return true;
            }
            else { return false; }
        }
    }
}

- Kunal May 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

wap to postfix to infix, not infix to postfix

- 123 May 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

<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>

- Anonymous May 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<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>

- Anonymous May 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using 2 stacks we can implement the postfix expre to infix expr

- huntur June 28, 2013 | Flag Reply


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