Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
490 views

For proper oa or interview experiences list of all top tech companies visit :

https://oa.desiqna.in

https://interview.desiqna.in

 

image

 

in Online Assessments by Expert (46,090 points) | 490 views

2 Answers

0 like 0 dislike
Best answer

I don't remember the exact question but it goes something like this:
You have some comic books and some coins, you can trade comic books for a fiction book or you can sell a comic book + coins needed to get a fiction book.
coinsOffered is the number of coins offered if you sell a comic book.
You want to get as many fiction books as possible.

 

You had 10 comic books, coins you have 10, coinsNeeded: 1, coinsOffered: 1.
You can trade all your comic books for fiction books in this case, you don't need to sell any.

 

You have 4 comic books, coins you have: 8, coinsNeeded: 4, coinsOffered: 3
You can trade 2 comic books for fiction books. If you try to sell one comic book, your coins will go up to 11 which is not enough for 3rd fiction book - so the answer is 2.

 

It seems like it needs a greedy solution but I couldn't get to it during the assessment. If you have seen the question before or were able to solve it, any pointers will be helpful

by Expert (46,090 points)
0 like 0 dislike
-------------- code --------------
def barterMarket(comicBooks, coins, coinsNeeded, coinsOffered):
mx = 0


curr = comicBooks * coinsOffered + coins
while curr >= coinsNeeded: # count max by selling
    curr -= coinsNeeded
    mx += 1

tmp_coins = coins
tmp_comicbooks = comicBooks    
tmp = 0
while tmp_coins >= coinsNeeded and tmp_comicbooks:
    tmp_coins -= coinsNeeded
    tmp_comicbooks -= 1
    tmp += 1
mx = max(mx,tmp)    

return mx



if name == "main":
print(barterMarket(4,8,4,3))
-------------- code --------------
by Expert (46,090 points)