Amazon Interview Question for Software Engineer in Tests






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

Take the combination of all the elements in the array, use them to find the sum of each set extracted. Still this can be optimized.

- Anonymous December 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think this can be solved using Dynamic Programming

- Anonymous December 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
		int[] a = { 5, 5, 10, 2, 3 };
		// int[] a={1, 2, 3, 4, 5};
		int i = find15Combi(a, 0, 15);
		System.out.println(i);

	}

	private static int find15Combi(int[] a, int start, int toSearch) {
		int i = 0;
		for (; start < a.length; start++) {
			if (a[start] == toSearch) {
				i++;
			} else if (a[start] < toSearch)
				i = i + find15Combi(a, start + 1, toSearch - a[start]);
		}
		return i;
	}

- verma.tech December 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I believe both the space and time complexity of this solution are non-polynomial, am I right?
Using dp, we can solve it in o(n^2) time and o(n) space.

- fentoyal December 30, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

you are right , it's recursion
can you please give algo using dynamic programming,

- verma.tech December 31, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I don't think dynamic programming applies here. There are total 2^5=32 combinations for 5 numbers. Dynamic programming doesn't reduce the number of subproblems, it only solves each subproblem atmost once. verma.tech's code doesn't hit same subproblem twice.

- Anil January 25, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is the code I used:

#include <stdio.h>

// a: array
// n: last index of a
// sum: desired sum
// curr: current sum
int subsetSum(int a[], int n, int sum, int curr) {
    if(curr==sum)
        return 1;
    if(curr>sum || n<0)
        return 0;
    return subsetSum(a, n-1, sum, curr+a[n]) + subsetSum(a, n-1, sum, curr);
}

int main() {
    int a[] = {5, 5, 10, 2, 3};
    printf("%d\n", subsetSum(a, 4, 15, 0));
    
    system("pause");
    return 0;
}

- Anil January 25, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args) {
int[] a = { 5, 5, 10, 2, 3 };
// int[] a={1, 2, 3, 4, 5};
int i = find15Combi(a, 0, 15);
System.out.println(i);


}



private static int find15Combi(int[] a, int start, int toSearch) {
int i = 0;
for (; start < a.length; start++) {
if (a[start] == toSearch) {
i++;
} else if (a[start] < toSearch)
i = i + find15Combi(a, start + 1, toSearch - a[start]);
}
return i;
}

- verma.tech December 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

works fine only if the input is sorted (ascending).

Also,u need to invoke this function for all the indexes of array and den add the final sum to have all the values

- Vishal December 28, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I tested this for variety of inputs and seems to be working fine.
input int[] a = { 5, 5, 10, 2, 3 }; is not sorted and it works for that and returns 4
also, we need not generate all combinations, if we can check mid-way that whether the current combi is valid one or not.

please update if u find some bug in above.

- verma.tech December 28, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think it does not have a polynomial time solution .. as it is a well known subsetsum problem

- m December 29, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@verma.tech

sry..was a lil premature.. works like a bliss ..

- Vishal December 29, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This doesn't work if the input contains 0s or negative numbers.

- sundar January 31, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

@fentoyal can u post ur solution

- Newbie January 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="java" line="1" title="CodeMonkey69130" class="run-this">int findSumComb(int a[], int sum)
{
int s[MAX_SUM]={1};
for (int i = 0; i < SIZEA; ++i)
for (int j = sum; j>=a[i]; --j )
s[j] += s[j-a[i]];
return s[sum];
}
</pre><pre title="CodeMonkey69130" input="yes">
</pre>

- Anonymous January 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is my solution. I dont know how to post the code so ...
Anyway, give u the main function too. u can check it if u like.
the limitation is all integers must be positive.
to handle both positive and negative, the algo can not be so elegant and compact. but the idea is similar.

#include <iostream>
using namespace std;
#define SIZEA 5
#define MAX_SUM 20
int main()
{
	int aa[] ={1,2,3,4,5};
	int bb[] = {5,7,10,2,3};
	cout << findSumComb( bb, 15) << endl;
  getchar();
  return 0;
}

- fentoyal January 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Another thing I want to clarify is that: precisely speaking, this is not a polynomial solution because its in O(n*sum) where sum is actually nonlinear with the n. Suppose we have 5 numbers: 100000, 200000, 3000000, 4000000, 500000. The time comp is O(5*150000). But in this case, since 15 is not that big, dp works perfectly well.

- fentoyal January 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

A small problem with your findSumComb code....For an input like the following:

int bb[] = {5,5,10,2,3};

The program returns 4 whereas the answer should be 6. Any insight or correction in ur algo?

- Rishabh January 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ignore the above remark. My bad!!

- Rishabh January 04, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@fentoyal - A very elegant solution! I too had a DP approach, but was far from this elegant!

- Lakshmi Ramakrishnan January 09, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

A very unique solution! Excellent!

- Anil January 25, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks fentoyal - what's MAX_SUM here?

- anon April 18, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int find_num_combinations(int a[])
{
  int num=3;
  int count=0;
  int sum=0;

  while(num<32)
  {
     if(num&(num-1)==0)
       num++;
     else
      {
        pos=0;
        sum=0;
        for(i=0;i<5;i++)
        {
           if((num>>i)&1)
            {
              sum+=a[pos];
             }            
           pos++; 
         }
         if(sum==15)
            count++;
       num++; 
     }
  }
  return count;
}

- Jesukumar January 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote
{{{ #include<stdio.h> void sum(int s,int i); int a[]={5,5,10,2,3},n=5,t=15,c=0; void main() { sum(0,0); printf("\n\n\t%d",c); } void sum(int s,int i) { if(i>n)return; if(s==t){c++;return;} sum(s+a[i],i+1); sum(s,i+1); } - Vengadeswaran C January 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int []array_Max={5, 5, 10, 2, 3};
int combine=0;
for (int i=0;i<array_Max.length;++i)
{

int j=i+1;
combine+=findCombination(array_Max,array_Max[i],j,15);
}
System.out.println(combine);

public static int findCombination(int []array, int sum, int iStart, int iTarget)
{
int n=0;
for (int i=iStart;i<array.length;++i)
{
if (sum+array[i]==iTarget)
{
n++;
}

n=n+findCombination(array,sum+array[i],++iStart,iTarget);
}

return n;
}

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

nt []array_Max={5, 5, 10, 2, 3};
int combine=0;
for (int i=0;i<array_Max.length;++i)
{

int j=i+1;
combine+=findCombination(array_Max,array_Max[i],j,15);
}
System.out.println(combine);

public static int findCombination(int []array, int sum, int iStart, int iTarget)
{
int n=0;
for (int i=iStart;i<array.length;++i)
{
if (sum+array[i]==iTarget)
{
n++;
}

n=n+findCombination(array,sum+array[i],++iStart,iTarget);
}

return n;
}

- Scott January 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// count how many different combinations that sum up to X
int countSum(vector<int> v, int X) {
  if (X == 0) { // termination case: this combination satisfied
    return 1;
  }
  if (v.empty()) { // termination case: this combination unsatisfied
     return 0;
  }
  int n = v.back();
  v.pop_back();
  if (n <= X) { // try two cases: with n or without n
    return countSum(v, X - n) + countSum(v, X);
  } else { // since n is too large, try only one case: without n
    return countSum(v, X);
  }
}

- maxxum7 January 11, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Its a subset sum problem

- koun January 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This problem can't have polynomial run time..
For an input like this { 1,1,1,1,1 } & sum = 5

You need to print all permutations .. so !! can't have linear time algo

- Time September 19, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

are you dumb !! answer for your input is 1 and it's not permutation .. combination !!!

- dude September 19, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

def sumupTo(a, s, sol):
  if len(a) > 0:
    if a[0] <= s:
      sumupTo(a[1:], s-a[0], sol+"+"+str(a[0]))
    sumupTo(a[1:], s, sol)
  elif s == 0: print sol

- amshali February 26, 2012 | 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