Amazon Interview Question for Software Engineer / Developers


Country: India
Interview Type: Written Test




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

void calExpr(char *str,int *Op1,char *Op2,int top1,int top2)
{
	int i=0;
	char c;
	for(;str[i];i++)
	{
		c=str[i];
		if(c=='(' || c=='+' || c=='-' || c=='*' || c=='/')
			Op2[++top2]=c;
		else if(c==')')
		{
			if(Op2[top2]=='(')
				top2--;
			else
			{
				cout<<"Invalid Expression ";
				return;
			}
		}
		else
		{
			if(Op2[top2]=='+' || Op2[top2]=='-' || Op2[top2]=='*' || Op2[top2]=='/')
			{
				int y=atoi(&c);
				int x=Op1[top1--];
				char ch=Op2[top2--];
				if(ch=='+')
					Op1[++top1]=x+y;
				else if(ch=='-')
					Op1[++top1]=x-y;
				else if(ch=='*')
					Op1[++top1]=x*y;
				else if(ch=='/')
					Op1[++top1]=x/y;

			}
			else
				Op1[++top1]=atoi(&c);
		}
	}
	while(top2>=0)
	{
		if(Op2[top2]=='+' || Op2[top2]=='-' || Op2[top2]=='*' || Op2[top2]=='/')
		{
			int y=Op1[top1--];
			int x=Op1[top1--];
			char ch=Op2[top2--];
			if(ch=='+')
				Op1[++top1]=x+y;
			if(ch=='-')
				Op1[++top1]=x-y;
			if(ch=='*')
				Op1[++top1]=x*y;
			if(ch=='/')
				Op1[++top1]=x/y;
		}
	}
	cout<<Op1[0];
}

int main()
{
	int Operator[5],top1=-1,top2=-1;
	char Operand[5];
	char str[20];
	cout<<"Enter the Infix Expression ";
	cin>>str;
	calExpr(str,Operator,Operand,top1,top2);
	return 0;
}

- Aashish June 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

we have to use bracket precedence. right?
not using BEDMAS would mean not even cering about the brackets

- Tushar June 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

enum OperatorType 
{
   Invalid,
   Add,
   Substract,
   Multiply,
   Divide
}

input: "5 + 3 * ( 6 - 4 )"
the result is f (exp, 0, 17); // 17 include '\0'

int f (char* exp, int st, int end)
{
   int a = INT_MAX;
   int b = INT_MAX;
   OperatorType op = Invalid;

   for (int i=st; i<end+1;)
   {
      if (IsNum (exp[i]))
      {
         if (op == Invalid)
         { 
            SetOperand (&a, exp[i]);
         }
         else
         {
            SetOperand (&b, exp[i]);
         }
         i++;
      }
      else (IsOperator (exp[i]))
      {
         SetOperator (&op, exp[i]); // translate exp[i] to operator
         i++;
      }
      else (IsLeftBracket (exp[i]))
      {
         int rightBracketPos = FindRightBracket (exp, i+2, end);
         int sub = f (exp, i+2, rightBracketPos-2); // +2, -2 is to skip the space and the current bracket

         if (op == Invalid)
         { 
             a = sub;
         }
         else
         {
             b = sub;
         } 
         
         i += rightBracketPos - i + 1; // go to the next space
      }
      else if (IsSpace (exp[i]) || exp[i] == '\0')
      {
         if (b != INT_MAX)
         {
            a = Compute (a, b, op);
            b = INT_MAX;
            op = Invalid;   
         }
  
         i++;
      }
   } 

   return a;  
}

void SetOperand (int *a, int e)
{
    if (*a == INT_MAX)
    {
       *a = e;
    }
    else
    {
       *a = (*a) * 10 + e;
    }
}

- jiangok2006 June 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// Expression.cpp : main project file.

#include "stdafx.h"

using namespace System;

typedef int (*operationFun) (int,int);

int add(int a, int b)
{
return a+b;
}
int sub(int a, int b)
{
return a-b;
}
int mul(int a, int b)
{
return a*b;
}

int div(int a, int b)
{
return a/b;
}

operationFun whatToDo(int val)
{
switch(val)
{
case '+':
return add;
case '-':
return sub;
case '*':
return mul;
case '/':
return div;
default:
return 0;
break;
}
}


int main(array<System::String ^> ^args)
{
//char *str = {"5 + 3 * ( ( 6 - 4 ) + ( 2 - 1 ) )"};
char *str = {"5 + 3 * ( 6 - 4 )"};
int stack[15] = {0};
int stackTop = 0;
int op1, op2, op3, val, nextVal, prevVal, prev1Val, prev2Val, prev3Val;
int s1, s2, s3;
int res;

int (*operation) (int,int) = 0;

while(*str != 0)
{
val = *str++;

*str++;

switch(val)
{
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
stack[stackTop++] = val - '0';
break;

case '+':
case '-':
case '*':
case '/':
operation = whatToDo(val);

nextVal = *str++;

*str++;

if(nextVal >= '0' && nextVal <='9')
{
res = (*operation)(stack[--stackTop], (nextVal-'0'));
stack[stackTop++] = res;
}

if(nextVal == '(')
{
stack[stackTop++] = val;
stack[stackTop++] = '(';
}

break;

case '(':
stack[stackTop++] = val;
break;
case ')':
prevVal = stack[--stackTop]; // will be a number.
prev1Val = stack[--stackTop]; // should be '('

if(stackTop == 0)
{
stack[stackTop++] = prevVal;
}
else
{
prev2Val = stack[--stackTop]; // check wheather is it a '(' or operator.

if(prev2Val == '(')
{
stack[stackTop++] = prev2Val;
stack[stackTop++] = prevVal;
}
else // (8*7)-9+8 ...???
{
prev3Val = stack[--stackTop]; // should be number.
operation = whatToDo(prev2Val);
stack[stackTop++] = operation(prevVal, prev3Val);
}
}
break;
default:
break;
}
}

Console::WriteLine(L"Result :: " + stack[stackTop-1]);
return 0;
}

- Sruthi Raghavendra June 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks a lot for the code. May I request you to write the algorithm as well that you followed here. That would really help to write this in another language too.

- Anonymous June 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Using binary tree?

- Test June 24, 2012 | 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