Interview Question
Java DevelopersCountry: UK
Interview Type: Written Test
//GREATER THAN FUNCTION
bool greaterThan(int N1, int N2){
if (MINUS(N1, N2)> 0) return true;
else return false;
}
//EQUALITY FUNCTION
bool equality(int N1, int N2){
if (MINUS(N1, N2)==0) return true;
else return false;
}
//INEQUALITY FUNCTION
bool inequality(int N1, int N2){
return !(equality(N1, N2));
}
//DIVIDE FUNCTION
int divide(int N1, int N2){
int res = 0, temp = N1;
while(temp>= N2){
N2 -= N1;
res++;
}
return res;
}
Although it looks correct (and maybe I am not seeing something), but you might endup having infinite recursion there. In greaterthan, you are reusing greaterthan to compare with 0, and in equatilty you are reusing equality. hence, in either cases (in my silly opinion) you will not hit the case to return false.
However, if I missed something, please point out, I am really interested in the solution to this.
Without restrictions let use minus and increment functions
Equality: a-b = 0
Inequality: a -b != 0
Greater than: a - b > 0
Divide: increment 0...b, and minus
Edited
Equality:
in c, return (a-b); false is returned for equality;// i am still thinking its java solution
Inequality: a -b !Equality 0
for b>a:
if(((a-b) >> 31)=1 and (a inequality b)) a is greater// for other data types (not 32 bit representation) similar shifting.
else b is greater
a/b
c= 0, temp= a;
while(temp>= b){
b-= a;
c++;
}
c is the answer
I am confused...
- Jackie May 21, 2013the assumption is
if (a-b)=0, then a=b, but we have not defined equal, that means we cannot use "if(a-b)==0)