Bloomberg LP Interview Question
Software Engineer / DevelopersNice! just a few typos:
with unit test:
#include <iostream>
int add(int p, int q)
{
if(q == 0)
return p;
int k, r;
k = p ^ q;
r = p & q;
return add(k, r<<1);
}
int main() {
printf("%d\n", add(4,7));
printf("%d\n", add(401,7));
printf("%d\n", add(14,7));
printf("%d\n", add(4,47));
printf("%d\n", add(4,-97));
}
We can make use of binary operations
for example
4 -> 0100
2 -> 0010
making an OR operation will give 0110 -> 6
so int p=4;
int q=2;
int r=p||q; will give 6
This is wrong. Take p=3, q=1 as an example. Also the logic or is | but not ||.
With loop.
for (int i=0; i<p; i++)
q++;
without loop.
int p,q;
add(p);
add(int val)
{
if (val==0)
return;
p++;
add(val-1);
}
the ques doesnt say that we cant use the - operator
so c = 0-b;
sum = a-c;
does this qualify or not
ADD(p,q)
- Anonymous February 27, 2008{
if(p==0)
return r;
else
int k,r;
k=p^q;
r=p&q;
ADD(k,r<<1);
}