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

Comment hidden because of low score. Click to expand.
0

thumbs up!

Comment hidden because of low score. Click to expand.
0

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

Comment hidden because of low score. Click to expand.
5

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

Comment hidden because of low score. Click to expand.
2

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

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

}

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

Comment hidden because of low score. Click to expand.
0

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 .

Comment hidden because of low score. Click to expand.
0

What does this line do?

y = carry << 1;

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

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.

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

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

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;

}

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

Comment hidden because of low score. Click to expand.
0

do not think it will work for input b=0

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

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

``````/*
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;
}``````

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;

}``````

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

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

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()
{

return 0;
}``````

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

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

}

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
{
}
}
else
{
}``````

}}

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

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

}

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

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

}

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

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

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

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

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

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

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

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

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

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

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;

}

}

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;

}

}

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

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.

### 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.