Google Interview Question
1,230 Interview Reviews |
Back to all Google Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Engineering Intern at Google:
Write this function (on a Google doc): /*How many ways can you make change given the following denominations? ie, if numCentsToMake is 6 and denominations is [25, 10, 5, 1], then it should return 2: either a nickel and a penny or 6 pennies.*/ int numWaysToMakeChange(int numCentsToMake, int[] denominations)
See more for this Google Software Engineering Intern Interview
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (2)
int ways[MAX];
int numWaysToMakeChange(int amount, int denominations[], int nden) {
memset( ways , 0 , sizeof ways );
sort( denominations, denominations + nden );
ways[0] = 1;
for (int i = nden-1 ; i >= 0 ; i--)
for (int am = denominations[i] ; am <= amount ; am++)
ways[am] += ways[am-denominations[i]];
return ways[amount];
}
Helpful Answer?
Yes |
No
Inappropriate?
To comment on this
question,
Sign In with Facebook or
Sign Up
2 of 2 people found this helpful
by Interview Candidate:
if num_cents_to_make == 0:
return 1
if len(denominations) == 0 or num_cents_to_make < 0:
return 0
return (make_change(num_cents_to_make - denominations[0], denominations) +
make_change(num_cents_to_make, denominations[1:]))