Linkedin Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
1
of 5 vote

private static void evaluatePostfixExpression() throws Exception {
		String s = "345+*612+/-";
		Stack<Integer> stack = new Stack<>();
		char[] array = s.toCharArray();
		int i = 0;
		while (i < array.length) {
			if (isOperatr(array[i])) {
				int b = stack.pop();
				int a = stack.pop();
				int c = getResult(a, b, array[i]);
				stack.push(c);
			} else {
				stack.push(array[i] - '0');
			}
			i++;
		}
		System.out.println(stack.pop());
	}

	private static int getResult(int a, int b, char operator) throws Exception {
		switch (operator) {
		case '+':
			return a + b;
		case '-':
			return a - b;
		case '*':
			return a * b;
		case '/':
			return a / b;
		case '%':
			return a % b;
		}
		throw new Exception("Illegal operator Exception");
	}

	private static boolean isOperatr(char c) {
		switch (c) {
		case '+':
		case '-':
		case '*':
		case '/':
		case '%':
			return true;
		}
		return false;
	}

- Rocko January 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Need to check for invalid inputs. Else the program will crash, given that the EmptyStackException is not handled.

- pankaj June 27, 2015 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#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 ;
}

- umesh garg January 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

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

- yokuyuki June 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

double eval(string& s)
{
stringstream strin(s);
while (strin >> word) {
if (is_op(word))
{
double x = s.top(); s.pop();
double y = s.top(); s.pop();
s.push( compute(x, y, word) );
}
else
s.push( atof(word.c_str()) );
}
}
return s.top();

- fafdu1992 October 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

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();
}

- Yi December 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

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");
    }
}

- ElegantCode March 02, 2014 | 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