Apple Interview Question
Software Engineer in TestsCountry: United States
Interview Type: In-Person
We can optimize this further by adding swap before the increment or decrement begins.
To exemplify, imagine a = 100000, b = 1 ==> then we would increment b 100000 times, instead we can introduce a swap and instead increment a by 1.
public static int sum(int a, int b){
if(a > b){
int temp = a;
a = b;
b = temp;
}
while(a > 0) { --a; ++b; };
while(a < 0) { ++a; --b; };
return b;
}
Sorry for missing the "have to use either ++ or --" condition, but here is an bit-wise way to do this and no need to consider the number is negative or positive
Sum of two bits can be obtained by performing XOR (^) of the two bits. Carry bit can be obtained by performing AND (&) of two bits.
no-recursive implementation:
int add(int x, int y)
{
while (y != 0)
{
int carry = x & y;
x = x ^ y;
y = carry << 1;
}
return x;
}
recursive implementation:
int add(int x, int y)
{
if (y == 0)
return x;
else
return add( x ^ y, (x & y) << 1);
}
U need to handle case when x>0 and y<0 . U might need to onterchange x and y during this case . Logic remains the same .
int add( int x, int y)
{
// both x and y are positive
if( x>=0 && y>=0){
while(x){
++y;
--x;
}
}
// x positive and y negative
else if( x>=0 && y<=0){
while(x){
++y;
++x;
}
}
//y positive and x negative
else if( x<=0 && y>=0 ){
while(x){
++x;
--y;
}
}
// both negative
else{
while(x){
--y;
++x;
}
}
}
It can be made more efficient by using the variable that has the smallest absolute value as a condition breaker.
int sum(int a,int b)
{
int s=0;
//if both are positive
if(a>=0&&b>=0)
{
s=a;
for(int i=0;i<b;i++)
{
++s;
}
}
//both negative
if(a<0&&b<0)
{
s=a;
for(int i =b;i>0;i--)
{
--s;
}
}
//one positive one negative
if((a>0&&b<0)||(a<0&&b>0))
{
if(a>0)
{
s=a;
for(int i=0;i>b;i--)
{
--s;
}
}
else
{
s=b;
for(int i=0;i>a;i--)
{
--s;
}
}
return s;
}//end of function
T add(T a,T b) {
if(b == 0)
return b;
if(b < 0)
return add(a, ++b);
return add(a, --b);
}
How about this:
Please comment
/*
Write code to sum 2 integer but u cant use a+b method,
you have to use either ++ or --.
How you will handle negative numbers.
*/
#include <stdio.h>
int main(void)
{
int a=-2,b=-1;
int i,j;
if(b < 0)
j = -b;
else
j = b;
for(i=1;i<=j;i++)
{
if(a<0)
{
if(b<0)
a--;
else
a++;
}
else
{
if(b<0)
a--;
else
a++;
}
}
printf("%d",a);
return 0;
}
int add(int a, int b)
{
if(a >= 0)
{
while(a)
{
b++;
a--;
}
return b;
}
else if(b >= 0)
{
while(b)
{
a++;
b--;
}
return a;
}
else
{
while(a < 0)
{
a++;
b--;
}
return b;
}
}
int main()
{
printf("5 + 6 = %d\n",add(5,6));
printf("5 - 6 = %d\n",add(5,-6));
printf("-5 + 6 = %d\n",add(-5,6));
printf("-5 + -6 = %d\n",add(-5,-6));
return 0;
}
class SolutionForSumOfTwoInteger {
public int sumOfTwoInteger(int a, int b) {
int negBothFlag = 1;
int max;
int sum;
if(a < 0 && b < 0) {
negBothFlag = -1;
if(a*-1 > b*-1) {
max = a*-1;
sum = b;
} else {
max = b*-1;
sum = a;
}
} else if( a < 0 && b >= 0) {
max = b;
sum = a;
} else if( a >= 0 && b < 0) {
max = a;
sum = b;
} else {
if(a > b) {
max = b;
sum = a;
} else {
max = a;
sum = b;
}
}
for(int i = 0 ; i < max; i++) {
if(negBothFlag == -1) {
sum--;
} else {
sum++;
}
}
return sum;
}
}
public class SumOfTwoInteger {
public static void main(String[] args) {
SolutionForSumOfTwoInteger mSol = new SolutionForSumOfTwoInteger();
System.out.println(mSol.sumOfTwoInteger(3, 5));
System.out.println(mSol.sumOfTwoInteger(-3, -5));
System.out.println(mSol.sumOfTwoInteger(3, -5));
System.out.println(mSol.sumOfTwoInteger(-3, 5));
System.out.println(mSol.sumOfTwoInteger(0, -5));
System.out.println(mSol.sumOfTwoInteger(-5, 0));
System.out.println(mSol.sumOfTwoInteger(0, 0));
}
}
Assuming a, b are integers.
- Filip Z. October 11, 2014