Microsoft Interview Question for Software Engineer in Tests






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

int divide(a,b) {
if (b == 0) print "Invalid";
else if (a < b) return 0;
else return(1 + (divide(a-b, b));
}

Test cases:
1. a = 0
2. b = 0
3. a < b
4. b =1
5. b > a

- SS June 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what if a<0 or b <0?
How about the following modification:
int divide(int a, int b)
{
if (b==0) throw "invalid";
else if (a<0 || b<0)
{
int x=a>0 ? a: -a;
int y=b>0 ? b: -b;
int z = divide(x, y);
return (a<0) ^ (b<0) ? -z : z;
}
else if (a<b) return 0;
else
return 1+divide(a-b, b);
}

- zz September 25, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

ask, Is a is upper value?
incase he says yes then
ans:
a - b != a

Note: a value cannot be devide by 0. i am figuring out b is 0 or not

- ba.ravikumar@gmail.com May 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi

How about these ? Please let me know if there are more.

1. is a>b ?
2. is b!=0 ?
3. are a and b of same or different types ?
4. If they are of different types, do they
atleast extend the Number class ?

- SSJ May 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if (b == 0) {
cout << "cannot divide by 0!" << endl;
return 0;
}
rest = a;
result = 0;
while (rest*a > 0) {
rest = a - b;
result = result + 1;
}
return result

- fanchyna May 24, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static void divideTwoNumbersWithoutDividingOperator(int x, int y)
{
int res = 0;
int rem = 0;
int sign = 1;

//check the sign
sign = (x < 0) ? -1 : 1;

x = x * sign;//change the sign to a positive number

if (x < y)
{
rem = x;
}

while ((x - y) > 0)
{
rem = x - y;
x = x - y;
++res;
}

res = res * sign;//reset the sign

Console.WriteLine("x/y = " + res);
Console.WriteLine("x%y = " + rem);
}

- Zhan June 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good one. But need to see if y is negative too

- Anonymous June 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Try each combination of:
a=0,a=1,a=-1,a=null(if possible) and b=0,b=1,b=-1,b=null
The above cases covers equivalence classes of -ve result,+ve result and invalid result(division by zero). These even follow boundary value analysis.

Do performance testing by setting a=2^32 and b=2

Floating point result converted to int result: Try a=3,b=2 o/p=1 and a=2,b=3 o/p=0
a>b
a<b
and a=b(all covered in one of the above test cases)

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

#include<stdio.h>
int div(int a,int b);
main()
{
int q,w,x;
scanf("%d %d",&q,&w);
x=div(q,w);
printf("%d",x);
}
int div(int x,int y)
{
int z=x,count=0;
while(z>=y)
{
z-=y;
count++;
}
return count;
}

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

Also take care if b is negative.
if(b<0) call res=divide(a,-b) & if a is negative call res=divide(-a,b). The answer is -res.
If both are negative call divide(-a,-b)

int divide(a,b) {
          if (b == 0) print "Invalid";
           else if (a < b) return 0;
           else return(1 + (divide(a-b, b));
}

- Aashish June 30, 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