Coin Change Problem: Python interview with a Google engineer

preview_player
Показать описание
In this mock interview, watch someone try to solve the coin change problem in Python with a Google engineer.

Рекомендации по теме
Комментарии
Автор

Haven't finished this video yet, but i must say i absolutely love how the interviewer is trying to get the interviewee on the right track with his questions. Wanted to clap out loud for the interviewer here.

darrorpsk
Автор

No.1 rule of coding in a team is;

Don't be smart. Keep it simple.

TheHaters
Автор

I've watched this Google Interviewer many times, and you can tell that he wants to set his interviewees up for success. You can tell he's doing this to become a better interviewer

soledapper
Автор

"i don't think this is gonna give me the optimal solution"
"it will, in fact"


10/10

xxoan.
Автор

These interviews are helping me focus on my own solutions while others talk about the same problem, thank you!

cthecheese
Автор

That interviewer sounds like such a lovely person. I'm glad the interviewee eventually twigged he wanted to hear "Least Common Multiple".

aaronpcjb
Автор

WHo ever created this webpage/channel, its a genius! thanks for sharing, will checkout the website later!

xReisk
Автор

He was making it more complicated than it really was

crlfff
Автор

Interviewee: "I don't know why this is happening"


Interviewer: "I don't know why this is happening"

Satialfactory
Автор

I would say rather than thinking about optimal solution straight off the bat go for actually solving the problem and then optimize your solution.

aznhomeboi
Автор

I really appreciate these. The discussion afterward was good too. There are lots of reasons a person might not perform well in an interview like this, irrespective of how good they would be at the job. Fitzgerald only wrote 5 novels, Koontz has written over 80 - speed isn't everything.

cleverclover
Автор

The interviewee algorithm for the second case (no nickels) is incorrect. The Google engineer almost caught it, but gave an incorrect counter example that worked by chanche. In the end didn't recognize the issue and accepted the solution as correct.
The counter example he could've used at 24:25 is 55, or 80, or a number of others.

For example with 55, following the interviewee incorrect algorithm:
* Two quarters, 5 leftover which can be given with 5 coins: 2 + 5 = 7
* 5 dimes, 5 leftover which can be given with 5 coins: 5 + 5 = 10
* 55 pennies with no leftover = 55

So his algorithm would return 7: 2 quarters and 5 pennies. The real solution for 55 is is 5: 1 quarter and 3 dimes. His solution is incorrect because he always tries the maximum amount of the current denomination and then checks the leftover, but in some cases (like 55) he shouldn't take the maximum of any denomination.

LeonardoDonelli
Автор

Interviewer: hello, thank y-
Me: ok so "Hello world"

adamgarcia
Автор

Loved the question.
Here's my answer with Recursion


#1 quarter - 25, 1 nickle (5), 3 pennies (1) [dime 10]
register = {'quarter':0, 'nickle':0, 'dime':0, 'pennies':0}


def getCents(num, coins = 0):
if (num == 0):
return register, coins

if(num >= 25):
div, mod = divmod(num, 25)
register['quarter'] = div
coins += div
coins = getCents(mod, coins)
elif(num >= 5):
div, mod = divmod(num, 5)
register['nickle'] = div
coins += div
_, coins = getCents(mod, coins)
else:
register['pennies'] = num*3
coins += num*3

return register, coins


getCents(31)

Nishant
Автор

34:50 Answer because the LCM(Least Common Multiple) of 10 and 25 is 50 -> basically you can use 2 quarter cents instead of 5 dimes if its 50, thats the break point

DestinyIsDecided
Автор

As many have correctly pointed out the dynamic solution is incorrect. This is easily seen for values 36, 37, 55, 80, Actually there is 2 mistakes in it: one I believe is simply a typo - in the innermost loop num_of_coins is indexed with [cents - coins_j * j ] instead it should be [i - coins_j * j]. This correction alone makes it so that for the given numbers and with later discussed optimization (bringing all values below 50 before using dp algorithm) it would produce correct results, while technically being still incorrect.


The more important logical problem is that the line "coins_j = i / j" is still essentially greedy. E.g. in case of 55 it will try with 2 quarters + num_of_coins[5] and on next iteration 5 dimes + num_of_coins[5], but the algorithm will never try 1 quarter + num_of_coins[30] (=3 dimes). Therefore it won't get the correct answer for arbitrary coin composition and sum paid.

haha-uifp
Автор

I took a slightly different approach to return a list of coin names, instead of their count (before watching the entire vid):
coins = {
"Quarter" : 25,
"Dime" : 10,
"Nickel" : 5,
"Pennies" : 1,
}


def num_coins(cents, coins):
exchange = []
rest = cents
coin_idx = 0

while rest > 0:
coin_name =
this_coin = (cents // coins[coin_name])
for coin in range(this_coin):
exchange.append(coin_name)

rest = cents % coins[coin_name]
cents -= this_coin*coins[coin_name]
coin_idx += 1

return exchange

alissondamasceno
Автор

This interviewee starts to code out without even discussing the next approach properly. Breaks the fundamental idea. But kudos to the interviewer, and you are really lucky if you get such a helping interviewer.

code-
Автор

you can go with [25, 10, 1], then pop out the first element, go with [10, 1], then go with [1]. then take the minimum

rishangprashnani
Автор

My interview at Google as a front-end developer was brutal. All they gave me was a Google Doc (no text editor or IDE). There was little interaction with the interviewer. The problem was in a very specific format and all he said was "that's not ok" or "that works".

Pulits
visit shbcf.ru