Amazon Interview Question for Senior Software Development Engineers


Country: United States
Interview Type: Written Test




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

Here's a solution I played with while reading about this:

public class coin
    {
        private static int[] values = { 1, 5, 10, 25, 50, 100 };
        public static int numCoinCombo(int value)
        {
            return findNumSolUnder(value, 0);
        }

        private static int findNumSolUnder(int value, int place)
        {
            if (place >= values.Count() || value < 0) return 0;
            if (value == 0) return 1;
            return findNumSolUnder(value - values[place], place) + findNumSolUnder(value, place + 1);
        }
    }

- Jack September 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Of course, figuring out how this works is more valuable.

- Jack September 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Understanding why it returns an incorrect result with value = 0 is arguably of equal importance.

- Shabadoo September 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

geekyjumps.blogspot.in/2013/09/combinations-of-coins.html

- Anonymous September 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thank you very much. I add a condition, I think it can reduce the nest, is that OK?

if(n < denom)

#include "stdafx.h"
#include <stdio.h>


int makeChange2(int n, int denom);
int getNextDenom(int denom);


int _tmain(int argc, _TCHAR* argv[])
{


	int n = 6, change = 0;
	change = makeChange2(n, 1);
	printf("input = %d, output = %d\n",n,change);

    n = 11;
	change = makeChange2(n, 1);
	printf("input = %d, output = %d\n",n,change);

	n = 26;
	change = makeChange2(n, 1);
	printf("input = %d, output = %d\n",n,change);

	n = 76;
	change = makeChange2(n, 1);
	printf("input = %d, output = %d\n",n,change);
	getchar();
	return 0;
}

int makeChange2(int n, int denom)
{
        //Base condition
        if(0 == n)
            return 1;
  
        if(n < 0 || 0 == denom)
            return 0;

		if(n < denom)
            return 0;
   
        int nextDenom = getNextDenom(denom); //get the next denominator
        //either take the current denominator or eliminate it
        return makeChange2(n - denom, denom) + makeChange2(n, nextDenom);
}
 
//Utility function to get the next denominator
int getNextDenom(int denom)
{
    if(1 == denom)
        return 5;
    else if(5 == denom)
        return 10;
    else if(10 == denom)
        return 25;
    else
        return 0;
}

- Anonymous September 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

a very classic divide-conquer and DP problem

- James Liu September 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can you provide the code by C or C++?

- Anonymous September 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

it is quite similar to the knapsack problem. if you reach the solution yourself, you will learn much more

- Miguel Oliveira September 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static void checkForOptions(int value, int[] coinArray,
ArrayList<Integer> selectedCoins, int headNumberPassed) {
if (headNumberPassed > 0) {
selectedCoins.add(headNumberPassed);
}
for (int i = 0; i < coinArray.length; i++) {
int headNumber = coinArray[i];
if (headNumber < value) {
int newNumber = value - headNumber;
checkForOptions(newNumber, coinArray, new ArrayList<Integer>(
selectedCoins), headNumber);
} else if (headNumber == value) {
selectedCoins.add(headNumber);
printSelectedCoin(selectedCoins);
} else {
return;
}

}

}

- Prateek Sharma September 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static void checkForOptions(int value, int[] coinArray, 
ArrayList<Integer> selectedCoins, int headNumberPassed) { 
if (headNumberPassed > 0) { 
selectedCoins.add(headNumberPassed); 
} 
for (int i = 0; i < coinArray.length; i++) { 
int headNumber = coinArray[i]; 
if (headNumber < value) { 
int newNumber = value - headNumber; 
checkForOptions(newNumber, coinArray, new ArrayList<Integer>( 
selectedCoins), headNumber); 
} else if (headNumber == value) { 
selectedCoins.add(headNumber); 
printSelectedCoin(selectedCoins); 
} else { 
return; 
} 

} 

}

- Prateek Sharma September 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is known as coin problem. It has two variations. One is concerned with the possibility of maximum number of ways to make a particular sum out of given number of values. The other attempts to find the minimum number of coins. Here, we have the minimum number of required coins. We can create an array that has 1...n-1 elements, where n denotes the sum we are interested in, along with a hash that keeps track of minimum combination so far (i.e., a DP as mentioned above).

int FindMinimumNumberOfCoins(int LumpSum)
{
        if(LumpSum <= 0)
            return 0;
        
	int *a=new int[LumpSum-1];
        for(i=1;i<=Sum-1;i++)
           a[i-1]=i;
	int sum=6,p,min,indx,i,n=sizeof(a)/sizeof(a[0]);
	int *c=new int[sum+1];
	memset(c,0,sizeof(c)*(sum+1));
	c[0]=0;
	for(i=0;i<n;i++)
		c[a[i]]=1; //c[a[i]]++; is better

	printf("\n");
	for(p=1;p<=sum;p++)
	{
		if(c[p]!=1)  //include this if MINIMUM NUMBER OF COINS/WAYS is needed
		{
			min=inf;
			indx=-1;
			for(i=0;i<n;i++)
			{
				if(a[i]<=p)
				{
					if(c[p-a[i]]<min) 
					{
						min=c[p-a[i]];
						indx=p-a[i];
					}
				}
			}
			if(indx!=-1)
				c[p]=c[indx]+1;
		}
	}

	printf("%d\n",c[sum]);
	delete [] c;
        delete [] a;
}

- Anonymous September 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static List<int> CoinChanges(int n)
{
int[] values = { 1, 5, 10, 25 };
List<int> ret = new List<int>();
for (int i = values.Length - 1; i >= 0; i--)
{
while (values[i] < n)
{
ret.Add(values[i]);
n = n - values[i];
}
}
return ret;
}

- Senthil September 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static List<int> CoinChanges(int n)
{
int[] values = { 1, 5, 10, 25 };
List<int> ret = new List<int>();
for (int i = values.Length - 1; i >= 0; i--)
{
while (values[i] <= n)
{
ret.Add(values[i]);
n = n - values[i];
}
}
return ret;
}

I/p: 86
o/p: 25,25,25,10,1

- Senthil September 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int getCombinationNumbers(int value){
if(value < 0) return -1;
if(value == 0) return 0;

List<Integer> coins = new ArrayList<Integer>();
coins.add(1);
coins.add(5);
coins.add(10);
coins.add(25);

return getCombs(value, coins);
}

public static int getCombs(int value, List<Integer> coins) {
if(value < 0) return 0;
if(value == 0 || coins.size() == 1) return 1;
List<Integer> currentCoins = new ArrayList<Integer>(coins);
int largestCoinIndex = coins.size() - 1;
if(value < currentCoins.get(largestCoinIndex)) {
currentCoins.remove(largestCoinIndex);
return getCombs(value, currentCoins);
}
int count = 0;
int coinNumber = value / currentCoins.get(largestCoinIndex);
int largestCoinsValue = currentCoins.remove(largestCoinIndex);
for(int i = 0; i <= coinNumber; i++) {
count += getCombs(value - i * largestCoinsValue, currentCoins);
}
return count;
}

- arcueidyang September 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

private static int combi(int N) {
		if (N >= 25) return 13 * combi(N - 25);
		if (N >= 10) return 4 * combi(N - 10);
		if (N >= 5) return 2;
		return 1;
	}

- ben September 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.HashMap;

public class Combinations {


public static HashMap<Integer,Integer> lookup = new HashMap<Integer,Integer>() ;
public static Integer[] currency = {25,10,5};

public static void main(String[] argv)
{
countCombinations(13);
}
public static boolean check()
{
int dimCount= lookup.get(10);
int nickleCount = lookup.get(5) ;
int quarterCount = lookup.get(25);
if((dimCount>1)||(nickleCount>1)||(quarterCount>1))
{
return true;
}
return false;
}


public static void countCombinations(int input)
{
int totalcount=1;
lookup.put(25,0);
lookup.put(10,0);
lookup.put(5,0);
lookup.put(1,0);
for(Integer x : currency)
{
if(x<=input)
{
lookup.put(x, input/x);
int reminder = input%x;
if(reminder>=10)
{
lookup.put(10,reminder/10);
reminder = reminder %10;
if(reminder>5)
{
lookup.put(5, reminder/5);
lookup.put(1, reminder%5);
}
}else if((reminder>=5)&&(reminder<10))
{
lookup.put(5,reminder/5);
lookup.put(1,reminder%5);
}else if(reminder<5)
{
lookup.put(1,reminder);
}
boolean flag= check();
while(flag)
{
if(lookup.get(25)>1)
{
lookup.put(25,lookup.get(25)-1);
lookup.put(10,lookup.get(10)+2);
lookup.put(5,lookup.get(5)+1);
totalcount+=1;
}
else if(lookup.get(10)>1)
{
lookup.put(10,lookup.get(10)-1);
lookup.put(5,lookup.get(5)+2);
if(lookup.get(5)>1)
{
totalcount+=lookup.get(5);
}
else
{
totalcount+=1;
}
}else if(lookup.get(5)>1)
{
totalcount+=lookup.get(5)-1;
lookup.put(5, 1);
}
flag = check();
}
totalcount+=1;
lookup.put(25,0);
lookup.put(10,0);
lookup.put(5,0);
lookup.put(1,0);
}
}
System.out.println(totalcount);
}

}

- I hope it will be easy to understand September 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.HashMap;

public class Combinations {


   public static HashMap<Integer,Integer> lookup = new HashMap<Integer,Integer>() ; 
   public static Integer[] currency = {25,10,5};

   public static void main(String[] argv)
   {
	   countCombinations(13);
   }
   public static boolean check()
   {
       int dimCount= lookup.get(10); 
       int nickleCount = lookup.get(5) ; 
       int quarterCount = lookup.get(25); 
       if((dimCount>1)||(nickleCount>1)||(quarterCount>1))
       {
           return true; 
       }
       return false;
   }
   
   
   public static void countCombinations(int input)
   {
       int totalcount=1; 
       lookup.put(25,0);
       lookup.put(10,0);
       lookup.put(5,0);
       lookup.put(1,0);
       for(Integer x : currency)
       {
           if(x<=input)
           {
               lookup.put(x, input/x); 
               int reminder = input%x; 
               if(reminder>=10)
               {
                   lookup.put(10,reminder/10); 
                   reminder = reminder %10; 
                   if(reminder>5)
                   {
                       lookup.put(5, reminder/5); 
                       lookup.put(1, reminder%5); 
                   }
               }else if((reminder>=5)&&(reminder<10))
               {
                   lookup.put(5,reminder/5); 
                   lookup.put(1,reminder%5);
               }else if(reminder<5)
               {
                   lookup.put(1,reminder); 
               }
               boolean flag= check();
               while(flag)
               {
                   if(lookup.get(25)>1)
                   {
                       lookup.put(25,lookup.get(25)-1); 
                       lookup.put(10,lookup.get(10)+2); 
                       lookup.put(5,lookup.get(5)+1); 
                       totalcount+=1; 
                   }
                   else if(lookup.get(10)>1)
                   {
                       lookup.put(10,lookup.get(10)-1); 
                       lookup.put(5,lookup.get(5)+2);
                       if(lookup.get(5)>1)
                       {
                    	   totalcount+=lookup.get(5);   
                       }
                       else
                       {
                    	   totalcount+=1;
                       }
                   }else if(lookup.get(5)>1)
                   {
                       totalcount+=lookup.get(5)-1; 
                       lookup.put(5, 1);
                   }
                   flag = check(); 
               }
               totalcount+=1; 
               lookup.put(25,0); 
               lookup.put(10,0); 
               lookup.put(5,0); 
               lookup.put(1,0); 
           }
       }
       System.out.println(totalcount); 
   }

}

- Anonymous September 28, 2013 | 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