Amazon Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

1. sum the first k element from array.
2. Start a loop from k+1 element to end.
3. inside the loop, add current element to the current sum and subtract (current ptr - k)th value from the sum.
4. Whenever sum is equal to ssum, keep that as result.

- Anonymous February 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Same here. Isn't that a variation of subset sum problem ?

- Anonymous February 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Guys, this is answer is right. Interviewer confirmed with me. I gave the same answer. Also keep in mind. subsets and subarrays are not same. elements of subarrays are contiguous.

- viv February 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

The solution given in the first reply seems to be working in O(n). Why bother DP here?

- Amber February 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 2 vote

Here is the code (this is a recursive solution, and quite obviously it is exponential). DP-fying it should be pretty easy given the recursive code:

The code is in C# and is 100% working. You can simply copy paste it in a Console Application and hit Run :)

public void SubsetSum()
        {
            int[] arr = { 7, 6, 5, 1, 11, 2, 9, 17, 3, 4, 0 };
            SubsetSum(arr, 3, 0, 9, 0, 0, "");
            Console.ReadLine();
        }
        private void SubsetSum(int[] arr, int k, int elementsPickedSoFar, int targetSum, int sumSoFar, int i, string subsetSum)
        {
            if (k >= arr.Length) return;

            if (i >= arr.Length)
            {
                if (elementsPickedSoFar == k && targetSum == sumSoFar)
                    Console.WriteLine(subsetSum);

                return;
            }

            if (elementsPickedSoFar == k)
            {
                if (targetSum == sumSoFar)
                    Console.WriteLine(subsetSum);

                return;
            }

            SubsetSum(arr, k, elementsPickedSoFar, targetSum, sumSoFar, i + 1, subsetSum);
            SubsetSum(arr, k, elementsPickedSoFar + 1, targetSum, sumSoFar + arr[i], i + 1, subsetSum + " " + arr[i].ToString());
        }

Let me explain the recurrence in English:

1. In each step of the recursion, you don't pick the current element from the array (denoted by i) and DO NOT add anything to the sum (denoted by sumSoFar)
2. OR, you pick the current element from the array (denoted by i) and it to the sum (denoted by sumSoFar)

3. That's it! All that is left are the bases cases, which are pretty self-explanatory, but to give a quick rundown:
3i. You terminate when i >= arr.Length (quite obviously)
3ii. OR the total elements that you have picked so far (refer to Step 1) is equal to 'k' (max elements you can pick).

- P.M February 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Forgot to mention, but this code assumes that the subset array can be made up of any combination of elements (they don't necessarily have to be contiguous).

- P.M February 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I dnt knw about C# but if i use static variable between the recursive call I could ignore the additional arguments in the function call right?

- p February 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You don't want to make elementsPickedSoFar, subsetSum and sumSoFar static variables. The reason for this is because you want each recursive call to have its own copy.

On a side note, the sumSoFar was introduced for code clarity. You can choose to remove that parameter and change the code logic slightly so that it terminates when targetSum == 0 and in each recursive call you do sumSoFar - arr[i] (in case you want to pick that element).

- P.M February 17, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
int maxSubArraySum(int a[], int size)
{
int max_so_far = 0, end = 0;
int i;
for(i = 0; i < size; i++)
{
end = end + a[i];
if(end < 0)
end = 0;

else if (max_so_far < end)
max_so_far = end;
}
return max_so_far;
}

/*Driver program to test maxSubArraySum*/
int main()
{
int a[] = {-2, -3, 4, -1, -2, 4, 5, -3};
int max_sum = maxSubArraySum(a, 8);
printf("Maximum contiguous sum is %d\n", max_sum);
getchar();
return 0;
}

- vishu February 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This solution gives max sum in the array of any size... but the question is, with given sub-array size(K elements) with given sum you need to find number of ways you can get the sum

- Anonymous February 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

My solution in Scala. It could be event shorter with recursive function but this way it is simpler.

def calculate(a:Array[Int], subArrSize:Int, expSum:Int):Int = {
    var res = 0;
    for (startPoint <- 0 to a.size) {
      val subArray = a.drop(startPoint).take(subArrSize);
      if (subArray.size == subArrSize && subArray.reduceLeft(_+_) == expSum) {
        res += 1
      }
    }
    return res;
  }

  val arr = Array(1,2,3,5,-2,3,0)
  println("Res:" + calculate(arr, 2, 3))

- Ivan Luzyanin February 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int count = 0;
int sum = 0;
for(int i=0; i<k; i++)
  sum += a[i];	// sum will hold sum of first k elements
for(int i=k; i<n; i++)
{
  sum = sum + a[i] - a[i-k];  // sum has 0 - (k-1) elements sum, next time we need to check
  if(sum == targetsum)       // 1 - k elements sum, for this we will add kth element and 
    count++;		            // substract 0 element (i-k)
}

- Anonymous February 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

For the very first time,you also need to check.

- yingdi1111 February 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;
import java.util.List;


public class ArrayBiggestSumPath {

List sumlist = new ArrayList();

List findsubset(int[] a,int sum,int index)
{
List allsublist = new ArrayList();
if(index<0)
{
allsublist.add(new ArrayList());
return allsublist;
}
int element = a[index];
allsublist = findsubset(a,sum,index-1);
List mothersubset= new ArrayList();
for (int i = 0; i < allsublist.size(); i++) {
List l = (List)allsublist.get(i);
List newlist=new ArrayList();
newlist.addAll(l);
newlist.add(element);
if(findsum(newlist)==sum)
sumlist.add(newlist);
mothersubset.add(newlist);
}
allsublist.addAll(mothersubset);
return allsublist;
}
int findsum(List list)
{
int sum=0;
for (int i = 0; i < list.size(); i++) {
sum+=(Integer)list.get(i);
}
return sum;
}
public static void main(String[] args) {
int a[]={4,5,8,3,54,23,44};
int sum=28;
ArrayBiggestSumPath o = new ArrayBiggestSumPath();
o.findsubset(a, 31, a.length-1);
System.out.println(o.sumlist);
}
}

- noname---Its a kind of dp problem February 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The Dp solution is for positive integers

- doctor.sai February 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.io.*;
import java.util.*;

public class AllSumSubset{
public void displaySubSet(int setN[]) {
int length = setN.length;
int i;
int sumofnumber =0;
int sumofaset = 0;
int sumofnumberinaset=0;
try {
//BufferedWriter writer = new BufferedWriter(new FileWriter(new File("Results")));
for (i = 0; i < (1 << length); i++) {
for (int j = length-1; j >-1; j--) // We don't need the last one
{

if ((i & (1 << j)) != 0)
{
// writer.write("" + setN[j] + " ");
sumofaset+=setN[j];
sumofnumberinaset+=1;
}
if(sumofaset>setN[length-1])
break;
}

Arrays.sort(setN);
if((Arrays.binarySearch(setN,sumofaset)>0)&&sumofnumberinaset!=1)
{
sumofnumber=sumofnumber+1;
//writer.write("ok");
}

sumofaset=0;
sumofnumberinaset=0;
// writer.write("\r\n");
}
// writer.close();
System.out.println(sumofnumber);
} catch (Exception e) {
}
}

public static void main(String[] args) {
AllSumSubset ss = new AllSumSubset();
int setN[] = { 3, 4, 9, 14, 15, 19, 28, 37, 47, 50, 54, 56, 59, 61, 70, 73, 78, 81, 92, 95, 97, 99};
Arrays.sort(setN);


System.out.println(Arrays.binarySearch(setN,7)<0);
ss.displaySubSet(setN);
}
}

- Haomin February 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is actually a variant of the subset problem. Just make a few modifications on the original algorithm as follows:

public static LinkedList<Integer> subset = new LinkedList<Integer>();
  public static void getSubset(int[] a, int i, int k, int ssum) {
    if (i == a.length) {
      if (subset.size() == k) {
        int sum = 0;
        for (int e : subset) {
          sum += e;
        }
        if (sum == ssum) {
          for (int e : subset) {
            System.out.print(e + " ");
          }
          System.out.println();
        }
      }
      return;
    }
    subset.add(a[i]);
    getSubset(a, i + 1, k, ssum);
    subset.removeLast();
    getSubset(a, i + 1, k, ssum);
  }

- Yue March 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Partition problem. Dynamic Programming solution mentioned in Wikipedia and keeping the number of elements in a sum additionally.

- Anonymous February 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

at first sight of the problem, i also think dynamic programming can be a solution. but i can not give a formula for it. may you explain the detail of your idea.

- zxyecn February 16, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Partition problem. Dynamic Programming solution mentioned in Wikipedia and keeping the number of elements in a sum additionally.

- Anonymous February 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

onestopinterviewprep.blogspot.com/2014/03/namespace-arrayproblem-write-function.html

- Wellwisher March 26, 2014 | 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