Tuesday, December 5, 2006

Google Interview Puzzle : 2 Egg Problem

My intention here is not to trouble Google interviewers. I was just collecting some classic puzzles and found this one and a small Google search showed me that this is a Google interview puzzle to my pleasant surprise. But many of the answers I found were either wrong or totally twisted. I am making no surety of the answer I give and I am open to your remarks or suggestion or corrections.

The Standard Problem in simple writing goes like this:

* You are given 2 eggs. * You have access to a 100-storey building. * Eggs can be very hard or very fragile means it may break if dropped from the first floor or may not even break if dropped from 100 th floor.Both eggs are identical. * You need to figure out the highest floor of a 100-storey building an egg can be dropped without breaking. * Now the question is how many drops you need to make. You are allowed to break 2 eggs in the process

If you are one of the people who likes to solve a puzzle before seeing the answer you must quit the blog now and come back later for checking the answer.

Now that this is a Google interview question I am taking the normal "Interview-Style" of solving a problem. Simply saying thinking aloud through the solution from worst to the best correcting the flows optimizing the solution or taking the 5-minute hard thinking acting pause to a problem, which you know already and just want to make your interviewer think that you are a challenge lover.

Solution

Drop the first egg from 50.If it breaks you can try the same approach for a 50-storey building (1 to 49) and try it from 25th floor. If it did not break try at 75th floor. And use linear search with the remaining portion of storey we need to test. For example if the first egg breaks at 50 we need to try all possibilities from 1 to 49.

Now this looks a feasible solution. In computer student's jargon do a binary search with first egg and linear search with the second one. Best case is log (100) and worst is 50.

Now the optimal solution for the problem is that you figure out that you will eventually end up with a linear search because you have no way of deciding the highest floor with only one egg (If you broke one egg and you have to find the answer among 10 all you can do is start from the lowest to the highest and the worst is the total number of floors). So the whole question grinds up to how to make use of the first egg to reduce the linear testing of the egg.

(For strict computer science students, well this problem can be solved using binary search on the number of drops needed to find the highest floor.)

Now let x be the answer we want, the number of drops required.

So if the first egg breaks maximum we can have x-1 drops and so we must always put the first egg from height x. So we have determined that for a given x we must drop the first ball from x height. And now if the first drop of the first egg doesn’t breaks we can have x-2 drops for the second egg if the first egg breaks in the second drop.

Taking an example, lets say 16 is my answer. That I need 16 drops to find out the answer. Lets see whether we can find out the height in 16 drops. First we drop from height 16,and if it breaks we try all floors from 1 to 15.If the egg don’t break then we have left 15 drops, so we will drop it from 16+15+1 =32nd floor. The reason being if it breaks at 32nd floor we can try all the floors from 17 to 31 in 14 drops (total of 16 drops). Now if it did not break then we have left 13 drops. and we can figure out whether we can find out whether we can figure out the floor in 16 drops.

Lets take the case with 16 as the answer

1 + 15 16 if breaks at 16 checks from 1 to 15 in 15 drops1 + 14 31 if breaks at 31 checks from 17 to 30 in 14 drops1 + 13 45 .....1 + 12 581 + 11 701 + 10 811 + 9 911 + 8 100 We can easily do in the end as we have enough drops to accomplish the task

Now finding out the optimal one we can see that we could have done it in either 15 or 14 drops only but how can we find the optimal one. From the above table we can see that the optimal one will be needing 0 linear trials in the last step.

293 comments:

Your blog is really excellent. It inspires the readers who has that great desire to lead a better and happier life. Thanks for sharing this information and hope to read more from you. Great information…

The maximum no. of attempts for the above problem is 6. The method is to drop the first egg at a floor with no. half of 100 i.e 50. If it breaks, throw another form 75th(as it is in the mid of 100), if it doesn't,throw it from the 25th floor. The algorithm for finding the maximum no. of attempts for an 'n' story building is- "2^x < n", whr x= the max power of 2 for which the value is less than n & x gives the value of the max. no of attempts.

P.S- if anyone has anything to ask,feel free to contact me. My id is usankrityayan@gmail.com.

This is one of the best post that I have ever read. You have provided a great piece of information. I will definitely share it with my other friends. Keep up the good work, I would to stay in contact with your posts.Dubai property

In worst case, it does NOT break even from 64th floor (i.e. we have already made 7 drops).So, Now try out the same methodology for 100-64 = 26 floors i.e. drop the egg from 65th (1), 66th(2), 68th(4), 72nd(8), 80th(16), 96th(32).Now, again in worst case, the egg does not break even from 96th floor. (i.e. total drops now became 7+6=13).

Guru Maa is one of the most famous and best astrologer in delhi with over 40years of experience. She is a vashikaran and balckmagic specialist. You can get your love back easily by blackmagic, vashikaran or pooja with her God gift expertise in this filed of religion and sprituality.

But the interviewer probably wants to hear you reason backwards in the 2 eggs case, and (if it's a tough interviewer) will probably ask you the 3 egg case after you crack the 2 egg case (pun intended), and then finally give you the n egg case.

So it is best not to immediately deliver the general algorithm in an interview. Better still, if you've heard it before you should own up and say you've heard it before - that scores you major integrity points.

Instead of going into programming... If u think practically, egg breaks if u drop it from your hand (approximate height 4-5 ft)Assuming height of a floor 10ft then there should be no integer solution of this question. Second thing is that, between 0 to 5 feet, there are infinite num of points. You have only 2 eggs with you whereas you need infinite numbers of eggs. Best thing you can do is boil them and have a nice breakfast. :)

Avoid surprises — interviews need preparation. Some questions come up time and time again — usually about you, your experience and the job itself. We've gathered together the most common questions so you can get your preparation off to a flying start.

You also find all interview questions at link at the end of this post.

Avoid surprises — interviews need preparation. Some questions come up time and time again — usually about you, your experience and the job itself. We've gathered together the most common questions so you can get your preparation off to a flying start.

You also find all interview questions at link at the end of this post.

The exercise of fingernail design has been around for the last 5000 years and can be tracked to the people of Indian who ornamented their claws with henna. Now go toward 1932, when the France organization Revlon launched its first fingernail enhance. It was available in an extensive range of colors and used pigmentation instead of colors. Since the Thirties, fingernail art has come a long way.

The exercise of fingernail design has been around for the last 5000 years and can be tracked to the people of Indian who ornamented their claws with henna. Now go toward 1932, when the France organization Revlon launched its first fingernail enhance. It was available in an extensive range of colors and used pigmentation instead of colors. Since the Thirties, fingernail art has come a long way.

The best way to recover data from broken, dead or water damaged iPhone is restoring backup file. Users can take the help of iTunes utility in order to backup or restore iPhone data with ease. In case if you are facing any kind of issue with iTunes then in such situation you will have to opt for third party iPhone Recover Software in order to recover all lost iPhone files.

The information you have deliver here is really useful to make my knowledge good. Thanks for your heavenly post. It is truly supportive for us and I have accumulated some essential data from this blog.CAT coaching in chennai

Smartphone cases or Mobile Covers come in many different forms such as plastic, leather or metal. You can purchase the best size cover because a wide range of numerous sizes and shapes covers are available in the market. They will be a perfect fit for your smartphone which will make your smartphone easy to carry and better in grip. Most importantly, smartphone cases or mobile covers are the most affordable way to give your smartphone a perfect protection which will allow you to avoid any kind of unnecessary fund expenditure or investment in repairing or replacing conditions due to bad handling. It will clearly save you a lot of money and it will increase the lifespan of your smartphone as well!

Thanks for the puzzle. I've done the 2 egg problem before but just showed my son. Also here's a classic riddle that I wanted to share with everyone. Let me know if you can figure it out: What Has One Eye But Cannot See

I wasn't aware that this kind of a post would be possible to find, on until i came across this post. It would wow you how easy it is to read and how hard it is to forget relevant information. That's the beauty of it. While at that, you can always find the best information on Web Pages Organizing Help by checking the link. The best is guaranteed.

"You are given two eggs", therefore the most attempts you can have is 2!? In any case the best way you can find out is to weigh the eggs, then push an egg against the ground until it cracks (whilst measuring the force needed to crack them). Then using the acceleration due to gravity calculate the speed at each floor and relate that to the impact force on the egg, then find the height needed to crack the egg, find the floor above this height.

This is a nice article here with some useful tips for those who are not used-to comment that frequently. Thanks for this helpful information I agree with all points you have given to us. I will follow all of them.

I in addition to my friends came reading the nice information and facts located on your site and then immediately developed a horrible feeling I never thanked the blog owner for those tips. All the ladies were definitely certainly happy to read through them and now have definitely been making the most of those things. Appreciate your getting very accommodating and also for deciding upon some exceptional ideas most people are really eager to learn about. My very own honest apologies for not saying thanks to earlier.height shoes for men

Cheating is something that is very, very hard to deal with in a relationship, but if you're looking for ways to win your partner's heart back after separation Pls contact dr_mack@yahoo.com, he restored my relationship and mine is perfect now