## Amazon Interview Question

Computer Scientists**Country:**United States

**Interview Type:**Phone Interview

c#.

general solution for any number of given variables.

Without if statements.

```
static bool AreAllEqual( int num, List<int> variables ) {
try{
int[] arr = new int[1];
variables.Add( num );
for( int i = variables.Count - 1; i > 0; i-- ) {
arr[ variables[ i ] ^ variables[ i - 1 ] ] = 0;
}
return true;
} catch( IndexOutOfRangeException ){
return false;
}
}
```

Since there are four variables to compare you can add all four variables a+b+c+d. If each has value 200, then the end result should be 800.

Do a left shift operation on the number 200 by 2 times to multiply the number by 4.

e.g 200 << 2

This is the most efficient way to multiply a number in a multiple of 2^x in just one cpu instruction. where x is the number of left shifts.

This will make the number 200 as 800.

You will need atleast one comparision. It does not matter whether the comparision is in a return statement or it is if/switch-case statement. I dont think there is any other way to compare.

```
#define TRUE 1
#define FALSE 0
int checkNumber(int a, int b, int c, int d)
{
if((a+b+c+d) == (200<<2)){
return TRUE;
}
else{
return FALSE;
}
}
```

num = 2, a = 1, b = 3, c =, d = 2 -> a + b + c + d = 4 * num yet a == num && b == num && c == num & d == num is false

```
#!/usr/bin/env python3
def solution(num, a, b, c, d):
return a + b + c + d == 4*num
# Your solution fail on this input
assert not solution(4, 1, 4, 4, 7)
```

guessing since doing

`return (num == a && num == b && num == c && num == d)`

would require 4 comparisons that doing a single bitwise operation like

`(a & b & c & d) == num`

would be faster?

```
#!/usr/bin/env python3
from functools import reduce
def bitwise_and(a, b):
return a & b
def solution(number, sequence):
return reduce(bitwise_and, sequence) == number
assert solution(4, [4, 4, 4, 4])
assert not solution(4, [1, 4, 4, 7])
assert not solution(4, [1, 2, 3, 4])
assert not solution(4, [-10, 0, 4, 10])
```

c#.

- zr.roman January 09, 2016