Google Interview Question for Software Engineer / Developers


Country: United States




Comment hidden because of low score. Click to expand.
2
of 4 vote

1) Use knapsack problem to find the minimum value(instead of maximum) for Weight W. In the original algo instead of taking maximum value take minimum value for each Array[i] , 0<i<=W.

2) Now for each value of n, find the minimum index i in Array such that i+w[n]> W and note the value as Array[i]+v[n]. Update this value if the old value is greater than the new value

- Rajesh January 25, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

nice

- nicks March 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

When looking for minimum, "original algo" will always return zero.
I'd suggest reviewing the solution.

- aset July 09, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The question in itself seems incomplete.

- Prajwal Rupakheti January 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

thealgorithmist.com/showthread.php/529-Min-Amount-not-possible-using-infinite-supply-of-coins-%28Unbounded-knapsack%29

- bartender January 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

The question in itself seems incomplete.

- Prajwal Rupakheti January 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Find minimum value of 'V' corresponding to total weight 'Wi', where Wi > W.

- xiang March 07, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

what about doing normal knapsack , but storing the coins that are actually going in . After that run through the coins until you find one that is not in knapsack. (Consider first sorting the coins)

- Kamy January 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

we can assume all weights wi are smaller than w, otherwise just return min(vi) among wi's that exceed w.
Find the max among wi, say w_max , then apply knapsack on w+w_max -1 to find the minimum value. Scan through the list from w+1 to w+w_max-1 to find the min value here(besides 0s) . The min value here should be the answer.

- bz January 28, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

TreeSet<Integer> possibleValues = …;

void fillAllPossiblevalues(List<Integer> values, List<Integer> weights, Integer weightLimit, int valueStored) {
	possibleValues.add(valueStored);

	for (int i =0 ; i<weights.size(); i++) {
		if (weightLimit < weights.get(i)) continue;
		fillAllPossibleValues(values,weights, weightLimit -weights.get(i), valueStored + values.get(i));
		
}
}

Now,
int minsoFar = 0;
for (Integer value : possibleValues) { //this will be in increasing order
	if (value!=++minsoFar) {
	return misoFar;
}
}

- lokendra0408 February 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

it should be

weights.get(i)

instead of

weights.get(0)

also, can u give a reason as to why Treeset is used for poosibleValues why not a normal HashLinkedSet

- astronaut February 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

as linkedHashSet has a lesser insert complexity as compared to treeset, and since you need all values in sorted order, you can just traverse the linkedhashset in order(as in your problem you are inserting them in ascending order only )

- astronaut February 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi Astronaut,

You are right about the weights.get(i) mistake.

Why I have used Treeset is because it will give you the elements in sorted order while you are iterating.

Whereas linkedHashSet will give you the order in which elements were inserted!

Notice the comment "//this will be in increasing order"

- lokendra0408 February 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

here is my solution. I think the complexity is bad.

If someone has better implementation.
Please give out. I will very appreciate.

int KnapSack(int capacity, int v[], int w[], int index, int num) {
	if(capacity<0 || index>=num)
		return 10000;

	if(capacity == 0)
		return 0;

	int a = v[index] + KnapSack(capacity-w[index], v, w, index, num);
	int b = KnapSack(capacity, v, w, index+1, num);
	return min(a, b); 
}

int SmallestImpossible(int v[], int w[], int capacity, int num) {
	int smallest = 10000, c = 0;

	for(int i=0; i<num; i++) {
		int tmp = KnapSack(capacity+w[i], v, w, 0, num);
		if(tmp < smallest) {
			smallest = tmp;
			c = capacity+w[i];
		}
		//cout << capacity+w[i] << " " << tmp << endl;
	}	
	return smallest;
}

- leo February 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Could you explain your logic here please?

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

so for the first part this is what I feel: if denominations start from 1 then there is no value that is not possible (cause the coins are unlimited). If the smallest denomination is >1 then 1 is the smallest value that is not possible.

- guru April 13, 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