## Amazon Interview Question

Quality Assurance Engineers**Team:**Kindle

**Country:**India

**Interview Type:**Written Test

```
var list = new List<int>() {1, 1, -11, 1, 12};
var getListCount = list.Count;
var isListCountOdd = getListCount % 2 == 0;
var getIntegerIncrement = 3;
int takeFromList;
if (isListCountOdd)
{
takeFromList = getListCount/2;
}
else
{
if (getListCount <= 5)
{
takeFromList = getListCount - getIntegerIncrement;
}
else
{
getIntegerIncrement++;
takeFromList = getListCount - getIntegerIncrement;
}
}
// Get left and right numbers
var leftNumbers = list.Take(takeFromList);
var rightNumbers = list.Skip(takeFromList).Take(getIntegerIncrement);
// Sum them up
var leftNumbersTotal = leftNumbers.Sum();
var rightNumbersTotal = rightNumbers.Sum();
Console.Write(leftNumbersTotal == rightNumbersTotal ? "Left == Right" : "Left != Right");
Console.ReadKey();
```

```
-- This solution is for list contains 4 or more count.
var list = new List<int>() {1, 1, -11, 1, 12};
var getListCount = list.Count;
var isListCountOdd = getListCount % 2 == 0;
var getIntegerIncrement = 3;
int takeFromList;
if (isListCountOdd)
{
takeFromList = getListCount/2;
}
else
{
if (getListCount <= 5)
{
takeFromList = getListCount - getIntegerIncrement;
}
else
{
getIntegerIncrement++;
takeFromList = getListCount - getIntegerIncrement;
}
}
// Get left and right numbers
var leftNumbers = list.Take(takeFromList);
var rightNumbers = list.Skip(takeFromList).Take(getIntegerIncrement);
// Sum them up
var leftNumbersTotal = leftNumbers.Sum();
var rightNumbersTotal = rightNumbers.Sum();
Console.Write(leftNumbersTotal == rightNumbersTotal ? "Left == Right" : "Left != Right");
Console.ReadKey();
```

PYTHON::

>>> def sum(l):

a = 0

d = 0

b = len(l)

c = b/2

for i in range(0, c):

a = a + l[i]

for j in range(c, ((c*2)+1)):

d = d + l[j]

if a == d:

print "both sides of sum is equal"

return a

elif a != d:

a = 0

d = 0

for i in range(0, c+1):

a = a + l[i]

for j in range(c+1, ((c*2)+1)):

d = d + l[j]

if a == d:

print "both sides of sum is equal at 2nd level"

return a

else:

print "both sides of sum is not equal"

return False

>>>

>>> sum([1,1,-11,1,12])

both sides of sum is equal

2

>>> sum([-11,1,12,1,1])

both sides of sum is equal at 2nd level

2

>>> sum([-11,1,12,1,0])

both sides of sum is not equal

False

>>>

```
public static void main(String[] args) {
int[] a = {1,0,1,12,-2,-8};
int sum1 = 0, sum2 = 0;
for(int i=1; i<a.length; i++) {
for(int j = 0; j<=i; j++) {
sum1 += a[j];
}
for(int k=i+1; k<a.length; k++) {
sum2 += a[k];
}
if(sum1 == sum2) {
System.out.println("Sum1: " + sum1);
for(int l =0; l<=i; l++) {
System.out.println(a[l]);
}
System.out.println("Sum2: " + sum2);
for(int l =i+1; l<a.length; l++) {
System.out.println(a[l]);
}
}
sum1 =0; sum2 = 0;
}
}
```

```
protected void findLeftAndRightSum(){
//int[] a = {1,0,1,12,-2,-8};
int[] a = {1, 1, -11, 1, 12};
int sum1 = 0, sum2 = 0, index = 0;
while(index<a.length){
sum1 = 0;
sum2 = 0;
for(int i=0; i<=index; i++){
sum1 = sum1+a[i];
}
for(int i=index+1; i<a.length; i++){
sum2 = sum2+a[i];
}
if(sum1==sum2){
break;
}else{
index++;
}
}
System.out.println("Common Sum : " + sum1);
}
```

```
protected void findLeftAndRightSum(){
//int[] a = {1,0,1,12,-2,-8};
int[] a = {1, 1, -11, 1, 12};
int sum1 = 0, sum2 = 0, index = 0;
while(index<a.length){
sum1 = 0;
sum2 = 0;
for(int i=0; i<=index; i++){
sum1 = sum1+a[i];
}
for(int i=index+1; i<a.length; i++){
sum2 = sum2+a[i];
}
if(sum1==sum2){
break;
}else{
index++;
}
}
System.out.println("Common Sum : " + sum1);
```

}

protected void findLeftAndRightSum(){

//int[] a = {1,0,1,12,-2,-8};

int[] a = {1, 1, -11, 1, 12};

int sum1 = 0, sum2 = 0, index = 0;

while(index<a.length){

sum1 = 0;

sum2 = 0;

for(int i=0; i<=index; i++){

sum1 = sum1+a[i];

}

for(int i=index+1; i<a.length; i++){

sum2 = sum2+a[i];

}

if(sum1==sum2){

break;

}else{

index++;

}

}

System.out.println("Common Sum : " + sum1);

}

```
private static boolean recArray ( int[] nums, int index, int sum1, int sum2 ) {
if ( index >= nums.length ) {
return sum1 == sum2;
}
int value = nums[index];
return (recArray(nums, index + 1, sum1 + value, sum2) || recArray(nums, index + 1, sum1, sum2 + value));
}
```

where index =0 at the beginning

The first method is just to simply go through the array and calculate the sums for all previous elements and all next elements, and compare them.

- sergey.a.kabanov September 18, 2016But is is too inefficient.

More efficient method is:

1). To calculate total sum of all elements.

Growing Sum =0

2). Going through the array, subtract elements from the total sum and add them to growing sum. Compare Growing Sum to the Total Sum. If they are equal - we found our element(s).