Introduction🛹

Hey guys, Today is day 15 of the 100 Days to** LinkedIn Challenge.**

Image for post

Free For Kindle Readers

If you are Preparing for your Interview. Even if you are settled down in your job, keeping yourself up-to-date with the latest **Interview Problems **is essential for your career growth. Start your **prep **from Here!

Last month, I have been researching to find out the Frequently asked problems from these Companies. I have compiled **100 **of these questions, I am not promising you that you will get these questions in your interview but I am confident that most of these “interview questions” have similar logic and employs the same way of thinking from these set of challenges.

Before we move on to the first problem, If you are wondering why I chose LinkedIn, Yahoo and Oracle over FAANG is because I have completed a challenge Focusing on Amazon and Facebook Interview Questions in this Series:

New Day, New Strength, New Thoughts🚀

Day 15 — Reverse Polish Notation🏁

AIM🏹

Evaluate the value of an arithmetic expression in Reverse Polish Notation.

Valid operators are +-*/. Each operand may be an integer or another expression.

Note:

  • Division between two integers should truncate toward zero.
  • The given RPN expression is always valid. That means the expression would always evaluate to a result and there won’t be any divide by zero operation.

Example🕶

Input: ["2", "1", "+", "3", "*"]
Output: 9
Explanation: ((2 + 1) * 3) = 9

Input: ["4", "13", "5", "/", "+"]
Output: 6
Explanation: (4 + (13 / 5)) = 6

Code👇

	class Solution {
	    public int evalRPN(String[] tokens) {
	        Stack<Integer> stack = new Stack<Integer>();
	        for(String element : tokens)
	        {
	            if(!"+-*/".contains(element))
	            {
	                stack.push(Integer.valueOf(element));
	                continue;
	            }

	                int  second= stack.pop();
	                int  first= stack.pop();
	                int ans = 0;
	                switch(element)
	                {
	                    case "+":
	                        ans = first + second;
	                        break;
	                    case "-":
	                            ans = first - second;
	                            break;
	                    case  "/":
	                            ans = first / second;
	                            break;

	                    case  "*":
	                            ans = first * second;
	                            break;

	                }    
	                stack.push(ans);
	        }
	        return stack.pop();
	    }
	}

Author: Akshay Ravindran

Algorithm👨‍🎓

  1. Create a Stack which will store the numbers in the reverse order.
  2. When you come across an operator. Pop the top two elements from the stack.
  3. I have used the second number as the first pop then I do not have to worry about changing the variables for subtraction and division separately because the order matters for those operations.
  4. Create a switch case which will act based on the **type **of the operator.
  5. For each switch case, perform their operations on the two variables and push the result into the stack again.
  6. At the end of the traversal, the element at the top of the stack is the result.🔚

#software-development #programming #java #interview #coding

How to evaluate Reverse Polish Notation?
1.50 GEEK