Microsoft Interview Question for Software Engineer in Tests






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

if both are negative numbers then
-2--3
-2+-3ll be the input right..

and pls tell which teamasked u this Q

- Anonymous February 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Not really.... I used the infix to prefix fundamentals where I could calculate any expression. The interviewer looked satisfied.

- Daniel Johnson February 14, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

can we use stringstream? then it is easy to do string to double change

- losedream February 11, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>

int cal(char *in)
{
int a,b;
char op;
sscanf(in,"%d%c%d",&a,&op,&b);
switch(op)
{
case '*':
return a*b;
case '-':
return a-b;
case '+':
return a+b;
case '/':
if(b!=0)
return a/b;
else
return -1;
}
return;
}
main()
{
char x[100];
scanf("%s",x);
printf("%d",cal(x));
}

- Anonymous September 16, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good. Thx!

- DiX November 01, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@anonymous
wondering if you would be interested in knowing some test cases to break your code.
Your code fials for these sample testcases:
# -123-5 (operand in front)
# -1234+_+ 123 (having multiple operands and/or spaces)
# 123+NULL (missing arguements - decision on how to handle this case)

- peace November 06, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

But what if the negative sign at the front? shouldn't we address that?

- Anonymous October 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

here are few things I would keep in mind while working on this problem. Basic functionality is partially implement by @anonymous.

we should ensure that <operand1><number><operand2><number> format is kept intact. where operand1 can be null. Only after this we should resolve the expression.

- peace November 06, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use polish notation.

- vimal November 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

m_string::cacluation_string(const string & s) {
if (s.empty() || s.length() < 3) {
return "";
}
if ((s[0] != '-') && (s[0] < '0') && (s[0] > '9')) {
return "";
}
string oper1, oper2;
for(int i = 1; i < s.length(); i++) {
if (s[i] == '+' || s[i] == '-') {
oper1 = s.substr(0, i);
oper2 = s.substr(i);
break;
}
}
int iOper1, iOper2;
std::istringstream iss(oper1);
iss >> iOper1;
std::istringstream iss2(oper2);
iss2 >> iOper2;
string sret = "";
std::stringstream out;
out << iOper1 + iOper2;
sret = out.str();
cout<<" --- "<<sret<<endl;
return sret;
}

- Anonymous December 26, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

m_string::cacluation_string(const string & s) {
if (s.empty() || s.length() < 3) {
return "";
}
if ((s[0] != '-') && (s[0] < '0') && (s[0] > '9')) {
return "";
}
string oper1, oper2;
for(int i = 1; i < s.length(); i++) {
if (s[i] == '+' || s[i] == '-') {
oper1 = s.substr(0, i);
oper2 = s.substr(i);
break;
}
}
int iOper1, iOper2;
std::istringstream iss(oper1);
iss >> iOper1;
std::istringstream iss2(oper2);
iss2 >> iOper2;
string sret = "";
std::stringstream out;
out << iOper1 + iOper2;
sret = out.str();
cout<<" --- "<<sret<<endl;
return sret;
}

- gang December 26, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int calculate(char *in)
{
    stringstream ss;
    ss << in;
    int x, y;
    char op;
    ss >> x >> op >> y;
    switch(op)
    {
              case '+':
                   return x + y;
              case '-':
                   return x - y;
              case '*':
                   return x * y;
              case '/':
                   return x / y;
              case '%':
                   return x%y;
              default:
                   return 0;
    }                  
}

int main()
{
    cout << "Result : " << calculate("-1*0") << endl;
}

- Mona March 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int calculate(char *in)
{
    stringstream ss;
    ss << in;
    int x, y;
    char op;
    ss >> x >> op >> y;
    cout << x << endl;
    cout << op << endl;
    cout << y << endl;
    switch(op)
    {
              case '+':
                   return x + y;
              case '-':
                   return x - y;
              case '*':
                   return x * y;
              case '/':
                   return x / y;
              case '%':
                   return x%y;
              default:
                   return 0;
    }                  
}

int main()
{
    cout << "Result : " << calculate("-1*0") << endl;
}

- mona March 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. Convert given expression to either postfix or prefix.
2. Use a stack and push operands until you get a operator.
3. Fetch top 2 operands from stack.
4. Calculate result using the operator.
5. Push the result to stack again.
6. Repeat steps 2 to 5 till the last character in prefix/postfix expression.

- Anilbabu June 13, 2018 | 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