Microsoft Interview Question for Interns


Team: Bing-Appex
Country: United States
Interview Type: In-Person




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

Seems like a linear programming problem to me. Let x1,x2...xn be the no of coins of each type required to make the given sum. So, we have to minimize x1+x2+...+xn with the constraints,
k1x1+k2x2+.....knxn=sum, k1..kn being the exchange values
x1, x2..xn are positive integers
x1<=m1, m1 is the no of available coins of 1st type..and so on
x2<=m2
....
xn<=mn

- alex January 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
2
of 4 vote

Assumption is that its possible to provide change. (For that we can assume that there is unit coin available)
We will have to define one objective for dispensing the coins.
"Machine would like to dispense as few coins as possible"

Then this problem can be either solved by Greedy approach or dynamic programming approach.

Greedy approach : At any given stage, use the highest valued coin possible. Lets suppose you have to make change for 98 units and you have two 50 unit coins, one 20 unit coin and four 10 unit coins and unlimited 1 unit coins.
1st Stage : Use one 50 unit. (we can not use 2 as that would exceed the given amount)
2nd Stage : We could have used two 20 unit ones but we have only one. Use one. Remaining amount is 28.
3rd Stage : Use two 10 unit ones.
4th Stage : Use 8 one unit coins.

Another approach is Dynamic Programming.

Assume that the coins are of the value v1, v2, vn. We have to make change for value j.

MinNoOfCoins(j) = min{for all v in [v1-vn] calculate MinNoOfCoins{ j - v} } + 1

- SR January 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

I am not sure if the greedy approach always works..

Consider this example - you have to cater 98 cents and you have 50*1 , 20*2, 15*1, 10*1, 1*8... (n*m means you have m instances of n cents)

Greedy answer - 50 * 1 + 20 * 2 + 1 * 8. Total Coins = 11

DP answer - 50*1 + 20*1 + 15*1 + 10*1 + 1*3. Total Coins = 7

- jasmeet@iastate.edu January 16, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes,it can be solved only with DP

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

if m1,m2......mn b d max no of coins available of each type of x1,x2....xn coins respectively.
bill amount =amt, amount given by d customer = tot.
so change to b given = x i.e. tot-amt
k1x1+k2x2....knxn=x where k1,k2...kn b d no of each coins required .
initially k1=k2=.....kn=0
if x1,x2....xn are sorted (assuming x1 is highest and xn is least)then
while(x>x1)
{
if(k1<=m1)
{
x=x-x1;
k1++;
}
}
while(x>x2)
{
if(k2<=m2)
{
x=x-x2;
k2++;
}
}
.
.
.
.
.
.while(x>xn)
{
if(kn<=mn)
{
x=x-xn;
kn++;
}
}

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

This is COIN CHANGE PROBLEM. (well know)

- smashit December 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

onestopinterviewprep.blogspot.com/2014/03/vending-machine-problem-dynamic.html

- Wellwisher March 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

You could change denomination array as you want. Please test it. Thank you:)

public class VendingMachine {
	
	static int[] deno = {20,15,10,5,2,1};
	
	public static void change(int amount){
		int[] tab = new int[amount + 1];
		tab[0] = 0;
		for(int i=1;i<=amount;i++){
			int min = Integer.MAX_VALUE;
			for(int k=0;k<deno.length;k++){
				if(i - deno[k]>=0){
					int tmp = tab[i-deno[k]] + 1;
					if(tmp < min){
						min = tmp;
					}
				}
			}
			tab[i] = min; 
		}
		System.out.println(tab[amount]);
		return;
	}
	
	public static void main(String[] args) {
		change(30);
	}

}

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

You have not considered the vending machine inventory, i.e the no of coins it has.

consider this ex : lets say vending machine has 3 (25 cents) and 3 (10 cents). and lets say it has to return 80 cents, so it has to return 2 (25 cents) and 3 (10 cents)

- praveen January 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

another case
suppose you have enough coins for each denomination(50, 15 ,10 ,1 )only four denomination.

not for the change of 71.

by above algo answer will be : 1*50, 1*15, 6*1...
While best combination is as : 1*50, 2*10, 1*1

- asheesh.goyal January 27, 2013 | Flag


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