Amazon Interview Question for Software Engineer / Developers






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

'solution' and 'denom'are Global variables or you can also pass them( solution needs to be passed by reference)

vector< vector<int> > solution;
vector<int> denom;

from main call as:
vector<int>sub;
subsum(6,sub,0);
and then

if( numelments(solution) == 0 ) "NOT POssible"
else "nO of WAys = " NUmEL(solution);

subsum(N,vector<int> subsol,el)
{
 subsol.push(el);
 N=N-el;
 if( N==0 )
      if( !find( subsol in solution ) ) solution.push(subsolution);
 
 for( every element E in denom ) 
	if( E <= N ) subsum(N,subsol,E);

 return;
}

find() method can be easily constructed i think

- Aditya October 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

this one is not efficient

- Aditya October 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

it can be solved in O(nk), where size of array is n and k is the desired sum.

use DP as used in sum of subset.

- Mridul Malpani October 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

it can be solved in O(nk), where size of array is n and k is the desired sum.

use DP as used in sum of subset.

- Mridul Malpani October 23, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

answer is not 6 it is 5

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

int main()
{
int s[]={1,2,5,10};
int n=6,m=4;
int r=count(n,s,m);
printf("%d",r);
return 0;
}
int count(int n,int *s,int m)
{
if ( n == 0 )
return 1;
if ( n < 0 )
return 0;
if ( m <= 0 && n >= 1 )
return 0;

return count( n,s, m - 1 ) + count( n - s[m-1],s, m );

}

- manni October 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think as you are not storing the solutions hence :
you will count the same solution as many
like

above method will count
1+5 and 5+1 hence 2 but in the example I think they counted it as single

- Aditya October 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry My bad :( i think i em confused a bit on your method. I think its fine

- Aditya October 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

pls check d above code
nd correct if wrng

- manni October 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@aditya
i hve tried it on few tast cases and its wrkng fine..

- manni October 25, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

its a simple dp question
first sort all the coins
int func(int n,int index=0){
if(n<0)
return 0;
if(!n) return 1;
if(dp[n][index]!=-1) return dp[n][index];
return dp[n][index]=func(n-arr[index],index)+func(n,index+1);
}

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

Sorry a small missing of termination condition
first sort all the coins
vector<int> arr;
int func(int n,int index=0){
if(n<0)
return 0;
if(!n) return 1;
if(index==sz(arr)) return 0;
if(dp[n][index]!=-1) return dp[n][index];
return dp[n][index]=func(n-arr[index],index)+func(n,index+1);
}

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

How abt generating different permutations which have the sum 6

- Aishwarya October 26, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I dont think it will work!

- madhav November 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int func(int[] d, int ind, int N) {
    if(N == 0)
        return 1;
    if(N < 0)
        return 0;
    int ways = 0;
    for(int i = 0; d[ind]*i < n; i++) {
        ways += func(d, ind+1, N-d[ind]*i);
    }
    return ways;
}

- Anonymous October 28, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry, one more condition after
"if(N < 0) return 0;"
Add
"if(ind >= d.length) return 0;"

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

Simple and efficient Solution!!

But, the "for" loop condition should be "d[ind]*i <= n".

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

Why do we need the for loop condition "d[ind]*i <= n" instead of a normal "i<n"? (Assuming n is length of denomination array)

- Anonymous October 31, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The time complexity wont be O(nk) in this solution.Thats the major drawback I find with this solution

- python.c.madhav November 01, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int coins[4] = { 1,2,5,10};
int noOfDenominations = 4;
void denominations(string s,int remSum, int lastUsedCoinIndex)
{
	if(remSum == 0 )
	{
		cout << s << endl;
		return;
	}else{
		for(int i = lastUsedCoinIndex ; i < noOfDenominations ; i++)
		{
			if(remSum >= coins[i])
			{
				char temp[10];
				itoa(coins[i],temp,10);
				denominations(s+temp,remSum-coins[i],i);
			}
		}
	}

}

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

excellent algorithm srikanth

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

NA

- weijiang2009 February 06, 2011 | 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