Apple Interview Question for Software Engineer in Tests


Country: United States
Interview Type: In-Person




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

Assuming a, b are integers.

function sum(a, b) {
  while(a > 0) { --a; ++b };
  while(a < 0) { ++a; --b };
  return b;
}

- Filip Z. October 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

thumbs up!

- Orion arm, Laniakea April 11, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

def funcAdd(a,b):
	return a++--b

- Shubham August 03, 2015 | Flag
Comment hidden because of low score. Click to expand.
5
of 5 votes

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;
	}

- coolguy September 27, 2015 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 votes

I think you need to use absolute values of a and b for the optimization to work for negative numbers as well:
if (abs(a) > abs(b)) {
swap(a, b);
}
...

- aman.wardak December 18, 2015 | Flag
Comment hidden because of low score. Click to expand.
3
of 3 vote

I think this will work also:

public int DumSum(int op1, int op2)
        {
            var diff = Math.Abs(op2);
            for (int i = 0; i < diff; i++)
            {
                if (op2 < 0)
                {
                    op1--;
                }
                else
                {
                    op1++;
                }
            }

            return op1;

}

- ehowitzer October 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 3 vote

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);
}

- mysqto October 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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 .

- shukad333 October 11, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What does this line do?

y = carry << 1;

How can I learn these operations to multiply or addition with bit operators?

- newbee October 13, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

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.

- Anonymous October 10, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int add(int x, int y)
{
int result = x;
if (y>=0)
{
for(int i=0; i < y; i++)
{ result++;}
return result;
}
else
{
for(int i=0; i<-y; i++)
{result--;}
return result;
}
}

- Anonymous October 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Anonymous October 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think this will work.

int findSum1(int a,int b)
{
while (b!=0)
{
if (b<0)
{
b++;
a--;

}
else
{
b--;
a++;

}

}

return a;

}

- solutionMaster October 11, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

T add(T a,T b) {
	if(b == 0)
		return b;
	if(b < 0)
		return add(a, ++b);
	return add(a, --b);
}

- Noobie October 12, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

do not think it will work for input b=0

- CompilerOne November 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

sum(int a, int b):
if (b<0):
for i in range(0,-b):
a--
else:
for i in range(0,b):
a++
print a

- sandeep October 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}

- sagar October 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int add(int a1, int a2){
		
		for(int i=0; i<a2; i++){
			
			if (a1>0)
			a1++;
			else{
				a1 = -a1;
				a1--;
				a1 = -a1;
			}
		}
		
		for (int i=0; i<-a2; i++){
			if (a1>0)
			a1--;
			else{
				a1= -a1;
				a1++;
				a1=-a1;
			}
		}
		
		return a1;
		
		
	}

- Anonymous November 07, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private void sum(int a, int b){
		while(a <0){
			b--;
			a++;
		}
		while( a >0){
			b++;
			a--;
		}
		
		System.out.println("Sum==="+b);
	}

- manas November 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int sumLimited(int a, int b) {
    if (b == 0) return a;
    else if (b > 0) return sumLimited(++a, --b);
    else return sumLimited(--a, ++b);
}

- Anonymous December 15, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}

- Anonymous December 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main(int argc, char **argv)
{
int no_a, no_b;

no_a = atoi(argv[1]);

no_b = atoi(argv[2]);

for(;no_b != 0; no_a++, no_b--);

printf("\nSum of the 2 no is %d\n", no_a);

return 0;
}

- disencd December 31, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int sum(int a,int b)
{

	if(a>=0 && b>=0)
	{
		for(int i=0;i<b;i++)
			a++;
		return a;
	}
	else if(a<0)
	{
		for(int i=a;i<0;i++)
			b--;
		return b;
	}
	else
	{
		for(it i=b;i<0;i++)
			a--;
		return a;
	}

}

- saumya.wipro April 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int ADDwithoutAddition(int i, int j) {
		
		if(j<=0){
		if(j==0)
		{
			return i;
		}
		else
		{
			return ADDwithoutAddition(--i,++j);
		}
		}
		else
		{
			return ADDwithoutAddition(++i,--j);
		}

}}

- milindhpatil April 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int ADDwithoutAddition(int i, int j) {
// TODO Auto-generated method stub
if(j<=0){
if(j==0)
{
return i;
}
else
{
return ADDwithoutAddition(--i,++j);
}
}
else
{
return ADDwithoutAddition(++i,--j);
}


}

- milindhpatil April 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int ADDwithoutAddition(int i, int j) {
// TODO Auto-generated method stub
if(j<=0){
if(j==0)
{
return i;
}
else
{
return ADDwithoutAddition(--i,++j);
}
}
else
{
return ADDwithoutAddition(++i,--j);
}


}

- milindhpatil April 17, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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));
    }
}

- Scorpion King April 21, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main(){
    int a = -3;
    int b = -4;
    
    if (a > 0) {
        while (a--) {
            b++;
        }
    }
    else {
        while (a++) {
            b--;
        }
    }
    
    cout<<b;
}

- Anonymous July 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main(){
int a = -3;
int b = -4;

if (a > 0) {
while (a--) {
b++;
}
}
else {
while (a++) {
b--;
}
}

cout<<b;
}

- rajdeep July 28, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def funcAdd(a,b):
	return a++--b

- Shubham August 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class AddNumbersWithOutPlusSign {


public static void main(String[] args) {

int a=-2 , b=3;
int n;

if(b < 0) n=-b;
else n=b;

for (int i = 0; i < n ; i++)
{
if(b < 0)
{
a--;
}
else a++;
}

System.out.println(a);
}
}

- Sateesh June 22, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class AddNumbersWithOutPlusSign {


public static void main(String[] args) {

int a=-2 , b=3;
int n;

if(b < 0) n=-b;
else n=b;

for (int i = 0; i < n ; i++)
{
if(b < 0)
{
a--;
}
else a++;
}

System.out.println(a);
}
}

- Sateesh June 22, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class AddNumbersWithOutPlusSign {


    public static void main(String[] args) {

        int a=-2 , b=3;
        int n;

        if(b < 0) n=-b;
        else      n=b;

        for (int i = 0; i < n ; i++)
        {
            if(b < 0)
            {
                a--;
            }
            else a++;
        }

        System.out.println(a);
    }
}

- Sateesh June 22, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class SpecificSum {



public static Integer calculateSpecialSum(int a,int b){

int minVal=0;
int maxVal=0;
if(a<=b){
minVal=a;
maxVal=b;
}else{
minVal=b;
maxVal=a;
}

for(int i=0;i<maxVal;i++){
minVal++;

}

return minVal;

}

}

- Anonymous May 06, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class SpecificSum {



public static Integer calculateSpecialSum(int a,int b){

int minVal=0;
int maxVal=0;
if(a<=b){
minVal=a;
maxVal=b;
}else{
minVal=b;
maxVal=a;
}

for(int i=0;i<Math.abs(maxVal);i++){

if(maxVal>0) {
minVal++;
}else{
minVal--;
}

}

return minVal;

}

}

- Hari May 06, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int sum(int a, int b) {
		  while(a > 0) { --a; ++b;};
		  while(a < 0) { ++a; --b;};
		  return b;
		}

- goyalnamisha2010 March 03, 2019 | 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