Linkedin Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str[12] ;
int stack[12] ;
int i,k=0;
int op1,op2;
char ch ;
printf("enter the string\n");
scanf("%s",str);
for(i=0;i<strlen(str);i++)
{
ch=str[i] ;
if(ch>='0' && ch<='9')
{
stack[k++]=ch-'0' ;
}
else
{
op1=stack[--k];
printf("\n%d",op1);
op2=stack[--k] ;
printf("\n%d",op2);
switch(ch)
{
case '+':
stack[k++]=(op2+op1) ;
printf("\n+ %d",stack[k-1]);
break ;
case '-':
stack[k++]=(op2-op1) ;
printf("\n- %d",stack[k-1]);
break ;
case '*':
stack[k++]=(op2*op1) ;
printf("\n* %d",stack[k-1]);
break ;
case '/':
stack[k++]=(op2/op1);
printf("\n/ %d",stack[k-1]);
break ;
}
}
}
printf("\n%d",(stack[--k]));
getch();
return 0 ;
}
Python version:
def evaluate_postfix(expression):
stack = []
split_expression = expression.split()
for chunk in split_expression:
try:
value = float(chunk)
stack.append(value)
except ValueError:
value1 = stack.pop()
value2 = stack.pop()
if chunk == '+':
result = value2 + value1
elif chunk == '-':
result = value2 - value1
elif chunk == '*':
result = value2 * value1
elif chunk == '/':
result = value2 / value1
elif chunk == '%':
result = value2 % value1
else:
raise SyntaxError('Invalid operator')
stack.append(result)
return stack.pop()
public int Postifix(String str) throws Exception {
Stack<Integer> stack = new Stack<Integer>();
int one = 0;
int two = 0;
for (int i = 0; i < str.length(); i++) {
switch (str.charAt(i)) {
case '+':
try {
one = stack.pop();
two = stack.pop();
} catch (Exception e) {
System.out
.printf("You can't make pop in an empty stack!\n");
}
stack.push(one + two);
break;
case '*':
try {
one = stack.pop();
two = stack.pop();
} catch (Exception e) {
System.out
.printf("You can't make pop in an empty stack!\n");
}
stack.push(one * two);
break;
case '-':
try {
one = stack.pop();
two = stack.pop();
} catch (Exception e) {
System.out
.printf("You can't make pop in an empty stack!\n");
}
stack.push(two - one);
break;
case '/':
try {
one = stack.pop();
two = stack.pop();
} catch (Exception e) {
System.out
.printf("You can't make pop in an empty stack!\n");
}
stack.push(two / one);
break;
default:
stack.push(Integer.parseInt("" + str.charAt(i)));
break;
}
}
return stack.pop();
}
public class EvaulatePostFix {
public static void main(String... args) {
String exp = "532+8*+";
int value = 0;
Stack<Integer> stack = new Stack<Integer>();
for (int i = 0; i < exp.length(); ++i) {
char c = exp.charAt(i);
if (c == '+' || c == '-' || c == '*' || c == '/') {
value = evaluate(c, stack.pop(), stack.pop());
stack.push(value);
} else {
stack.push(Integer.parseInt(c + ""));
}
}
System.out.println("Value: " + value);
}
public static int evaluate(char operator, int operand1, int operand2) {
if (operator == '+')
return operand1 + operand2;
else if (operator == '/')
return operand1 / operand2;
else if (operator == '-')
return operand1 - operand2;
else if (operator == '*')
return operand1 * operand2;
else
throw new RuntimeException("Unsupported operator");
}
}
- Rocko January 18, 2013