Amazon Interview Question for SDE1s


Team: Machine learning
Country: India
Interview Type: Phone Interview




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

I don't understand the question. If it's simply the sum of the left == sum of the right, then no split would make your example of [-1, 100, 1, 98, 1] work. That question would actually be fairly difficult to do and would require multiple passes.

If the question is at what point does some left slice and some right slice have the same sum, then simply have a left sum pointer and a right sum pointer.

- Josh January 23, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static int FindIndexWhereLeftSumEqualRightSum(int[] arr)
        {
            if (arr == null || arr.Length == 0)
                return -1;
            int i = 1;
            int j = arr.Length - 2;
            int leftSum = arr[0];
            int rightSum = arr[arr.Length-1];
            while (i < j)
            {
                if (leftSum < rightSum)
                {                    
                    leftSum += arr[i];
                    i++;
                }
                else
                {                    
                    rightSum += arr[j];
                    j--;
                }
            }

            if (leftSum == rightSum)
            {
                return leftSum;
            }
            return -1;
        }

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

Java
Complexity: O(n)

public class SumOfLeftEqualsSumofRight {

	
	public static void main(String[] args) {
		int arr[] = {-1, 100, 1, 98, 1};
		SumOfLeftEqualsSumofRight sumOfLeftEqualsSumofRight = new SumOfLeftEqualsSumofRight();
		int totalSum = getSum(arr);
		System.out.println(sumOfLeftEqualsSumofRight.getElementWithEqualSum(arr,totalSum));
	}
	
	public static int getSum (int []arr){
		int sum=0;
		for(int curr:arr){
			sum=sum+curr;
		}
		return sum;
	}
	
	public int getElementWithEqualSum(int arr[], int sum){
		if (arr == null || arr.length == 0)
            return -1;
		int leftTotal = 0;//arr[0];
		int rightTotal = sum-arr[0];
		for(int i=1;i<arr.length;i++){
			leftTotal = leftTotal + arr[i-1];
			rightTotal = rightTotal - arr[i];
			if(leftTotal==rightTotal){
				return i;
			} 
		}
		return -1;
	}
}

- Prateek January 25, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

it is a phone interview question and should not complicate by aping solutions of similar type of problem

int findIndexHalfSum(int a[], int sz)
{
  int lsum = 0, rsum = 0;

  for (int i = 1; i++ ; i<sz) rsum +=a[i];
  
  for (int i = 1; i++ ; i<=sz-2) 
  {
    lsum +=a[i-1]; rsum -= a[i];
  
    if (lsum == rsum) return i;
  }

  return -1;
}

- Obama February 06, 2015 | 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