Jelly Beans Jars Puzzle-Asked in Microsoft interview

Question:

You have three jars that are all mislabeled. One contains peanut butter jelly beans, another grape jelly jelly beans and the third has a mix of both (not necessarily half-half mix).

How many minimum jelly beans would you have to pull out to find out how to fix the labels on the jars?

Labels on jars are as follows

Jar 1: Peanut butter

Jar 2: Grape

Jar 3: P.b. / Grape

Solution: 1

Only one jelly bean from the p.b./grape jar will do the trick.The trick here is to realize that every jar is mislabeled. Therefore you know that the peanut butter jelly bean jar is not the peanut butter jelly bean jar and the same goes for the rest.
You also need to realize that it is the jar labeled p.b./grape, labelled as the mix jar, that is your best hope. If you choose a jelly bean out of there, then you will know whether that jar is peanut butter or grape jelly jelly beans. It can’t be the mix jar because i already said that every jar is mislabeled.
Once you know that jar 3 is either peanut butter, or grape jelly, then you know the other jars also. If it is peanut butter, then jar 2 must be mixed because it can’t be grape (as its labeled) and it can’t be peanut butter (that’s jar 3). Hence jar 1 is grape.
If jar 3 is grape, then you know jar 1 must be the mix because it can’t be p.b. (as its labeled) and it can’t be grape (that’s jar 3). Hence jar 2 is peanut butter.

Thanks for Reading

Placewit grows the best engineers by providing an interactive classroom experience and by helping them develop their skills and get placed in amazing companies.

Learn more at Placewit. Follow us on Instagram and Facebook for daily learning.

--

--

--

Upskilling students for tech placements!

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

鬼滅之刃劇場版無限列車篇Kimetsu no Yaiba 看線上高清完整版影

Sending Threaded Emails Using Rails ActionMailer

Building Forms in Flutter

Microservice (part 5)

Engineering culture at Thread

Final Project of Scrumming

Infrastructure As Code

Implementing an Operating System #1

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Placewit

Placewit

Upskilling students for tech placements!

More from Medium

Scattered Rice Coding Question

Leetcode 862. Shortest Subarray with Sum at Least K

Do not ask this question in Coding Interview

Interesting DS/ALGO — real life Software Engineering interview Question