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

- samuel February 22, 2014 | Flag Reply
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)

- kr.neerav February 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

This is brilliant stuff

- Pras February 22, 2014 | Flag
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.

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

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.

- kr.neerav February 22, 2014 | Flag
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;
}
}
}

- Poovanderan February 22, 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