Microsoft Interview Question
Software Engineer in Testsprivate 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);
}
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)
int divide(a,b) {
- SS June 01, 2011if (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