## Interview Question

Country: United States

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

Define a hashmap H for all possible sums.
assume coin values are stored in an array A,

``````for each element e in A
for each v in H
if e+v is not in H, add it into H``````

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

1) Use recursion to get all combinations of coins (2^n) and hashmap to store unique sum values.

``````combination(sum,i)
if i == no of elements in array
//a particular combination of coins reached
H[sum] = sum
return
//include element i in the sum
combination(sum+A[i],i+1)
//do not include element i in the sum
combination(sum, i+1)``````

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

This is brilliant stuff

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

Is there a DP solution to the problem ? This looks like the coin change problem.

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

Interesting thought about DP problem. In DP at each step we store one of the many possible outputs. However in this case we will have to save all the possible outputs at each step. So i guess the space complexity will be exponential.

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

public class Coins
{
static public String[] aCoins = {"5","10","20","50"};

public static void main(String[] args)
{
Coins c = new Coins();
for(int i=0; i<aCoins.length; i++)
{
for(int j=i+1; j<aCoins.length; j++)
c.printComb(aCoins[i], aCoins[j], j+1);
}
}

public static void printComb(String a, String b, int i)
{
System.out.println(a + "+" + b);
String new1 = a+"+"+b;
while(i< aCoins.length)
{
printComb(new1, aCoins[i], i+1);
i = i+1;
}
}
}

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.

### 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.