Puzzles as Programmer Interview Question

download Puzzles as Programmer Interview Question

of 32

Transcript of Puzzles as Programmer Interview Question

  • 8/10/2019 Puzzles as Programmer Interview Question

    1/32

    Username

    PasswordLog in

    Remember Me

    Forgot your password?

    Create an account

    Search

    Home ExamsStudy guides

    Jobs Forums BooksWho we areContact Us

    Puzzle s as Programmer Interview Question

    User Rating: / 0Poor Best Rate

    Study guides - Progammer Interview QuestionType

    Article IndexPuzzles as Programmer Interview QuestionCutting a cake into 8 piecesMinimum guesses to find a # from 1 to 1000three-switches-three-bulbsLockers Puzzle8 Pennies Find Lightest 7 equalMeasure weight of an elep hant with no scaleMeasure 4 liters

    25 horses 5 lanes 3 fastest puzzleSame national champion 65 year spanRope Bridge Puzzle Four People One Flashlight8 marbles puzzle find heaviest8 marbles riddle generalize solution2 Fuses Problem Measure 45 MinutesTrain Escape Puzzle 2 boysAll Pages

    http://www.brainbeez.com/index.php/index.php?option=com_content&view=article&id=1838%3Apuzzles-as-programmer-interview-question&catid=234%3Aprogammer-interview-questiontype&Itemid=59&limitstart=13http://www.brainbeez.com/index.php/index.php?option=com_banners&task=click&bid=7http://www.brainbeez.com/index.php/index.php?option=com_user&view=reset#contenthttp://www.brainbeez.com/index.php/index.php?option=com_content&view=article&id=1838%3Apuzzles-as-programmer-interview-question&catid=234%3Aprogammer-interview-questiontype&Itemid=59&showall=1http://www.brainbeez.com/index.php/index.php?option=com_content&view=article&id=1838%3Apuzzles-as-programmer-interview-question&catid=234%3Aprogammer-interview-questiontype&Itemid=59&limitstart=14http://www.brainbeez.com/index.php/index.php?option=com_content&view=article&id=1838%3Apuzzles-as-programmer-interview-question&catid=234%3Aprogammer-interview-questiontype&Itemid=59&limitstart=13http://www.brainbeez.com/index.php/index.php?option=com_content&view=article&id=1838%3Apuzzles-as-programmer-interview-question&catid=234%3Aprogammer-interview-questiontype&Itemid=59&limitstart=12http://www.brainbeez.com/index.php/index.php?option=com_content&view=article&id=1838%3Apuzzles-as-programmer-interview-question&catid=234%3Aprogammer-interview-questiontype&Itemid=59&limitstart=11http://www.brainbeez.com/index.php/index.php?option=com_content&view=article&id=1838%3Apuzzles-as-programmer-interview-question&catid=234%3Aprogammer-interview-questiontype&Itemid=59&limitstart=10http://www.brainbeez.com/index.php/index.php?option=com_content&view=article&id=1838%3Apuzzles-as-programmer-interview-question&catid=234%3Aprogammer-interview-questiontype&Itemid=59&limitstart=9http://www.brainbeez.com/index.php/index.php?option=com_content&view=article&id=1838%3Apuzzles-as-programmer-interview-question&catid=234%3Aprogammer-interview-questiontype&Itemid=59&limitstart=8http://www.brainbeez.com/index.php/index.php?option=com_content&view=article&id=1838%3Apuzzles-as-programmer-interview-question&catid=234%3Aprogammer-interview-questiontype&Itemid=59&limitstart=7http://www.brainbeez.com/index.php/index.php?option=com_content&view=article&id=1838%3Apuzzles-as-programmer-interview-question&catid=234%3Aprogammer-interview-questiontype&Itemid=59&limitstart=6http://www.brainbeez.com/index.php/index.php?option=com_content&view=article&id=1838%3Apuzzles-as-programmer-interview-question&catid=234%3Aprogammer-interview-questiontype&Itemid=59&limitstart=5http://www.brainbeez.com/index.php/index.php?option=com_content&view=article&id=1838%3Apuzzles-as-programmer-interview-question&catid=234%3Aprogammer-interview-questiontype&Itemid=59&limitstart=4http://www.brainbeez.com/index.php/index.php?option=com_content&view=article&id=1838%3Apuzzles-as-programmer-interview-question&catid=234%3Aprogammer-interview-questiontype&Itemid=59&limitstart=3http://www.brainbeez.com/index.php/index.php?option=com_content&view=article&id=1838%3Apuzzles-as-programmer-interview-question&catid=234%3Aprogammer-interview-questiontype&Itemid=59&limitstart=2http://www.brainbeez.com/index.php/index.php?option=com_content&view=article&id=1838%3Apuzzles-as-programmer-interview-question&catid=234%3Aprogammer-interview-questiontype&Itemid=59&limitstart=1http://www.brainbeez.com/index.php/index.php?option=com_content&view=article&id=1838%3Apuzzles-as-programmer-interview-question&catid=234%3Aprogammer-interview-questiontype&Itemid=59http://www.brainbeez.com/index.php/index.php?option=com_content&view=category&id=234:progammer-interview-questiontype&Itemid=59http://www.brainbeez.com/index.php/index.php?option=com_content&view=section&id=10&Itemid=59http://www.brainbeez.com/index.php/index.php?option=com_mailto&tmpl=component&link=aHR0cDovL3d3dy5icmFpbmJlZXouY29tL2luZGV4LnBocC9pbmRleC5waHA/dmlldz1hcnRpY2xlJmlkPTE4MzglM0FwdXp6bGVzLWFzLXByb2dyYW1tZXItaW50ZXJ2aWV3LXF1ZXN0aW9uJm9wdGlvbj1jb21fY29udGVudCZJdGVtaWQ9NTk=http://www.brainbeez.com/index.php/index.php?view=article&catid=234%3Aprogammer-interview-questiontype&id=1838%3Apuzzles-as-programmer-interview-question&tmpl=component&print=1&page=&option=com_content&Itemid=59http://www.brainbeez.com/index.php/index.php?view=article&catid=234%3Aprogammer-interview-questiontype&id=1838%3Apuzzles-as-programmer-interview-question&format=pdf&option=com_content&Itemid=59http://www.brainbeez.com/index.php/index.php?option=com_content&view=article&id=1838:puzzles-as-programmer-interview-question&catid=234:progammer-interview-questiontype&Itemid=59http://www.brainbeez.com/index.php/index.php?option=com_banners&task=click&bid=7http://www.brainbeez.com/index.php/index.php?option=com_contact&view=contact&id=1&Itemid=105http://www.brainbeez.com/index.php/index.php?option=com_content&view=article&id=330&Itemid=94http://www.brainbeez.com/index.php/index.php?option=com_content&view=article&id=331&Itemid=93http://www.brainbeez.com/index.php/index.php?option=com_fireboard&Itemid=92http://www.brainbeez.com/index.php/index.php?option=com_jobline&Itemid=90http://www.brainbeez.com/index.php/index.php?option=com_content&view=section&id=10&Itemid=59http://www.brainbeez.com/index.php/index.php?option=com_lms&Itemid=84http://www.brainbeez.com/index.php/http://www.brainbeez.com/index.php/index.php?option=com_user&task=register#contenthttp://www.brainbeez.com/index.php/index.php?option=com_user&view=reset#contenthttp://www.brainbeez.com/index.php/index.php
  • 8/10/2019 Puzzles as Programmer Interview Question

    2/32

    Puzzles as Interview Questions

    Like it or not, puzzles are a big part of interview questions for programmers these days.

    Whether they really tell your employer anything revealing about your intelligence ishighly debatable (and highly unlikely in our opinion). But, here we present somepuzzles that weve come across. Just try to have fun with them!

    1. You have a birthday cake and have exactly 3 cuts to cut it into 8 equal pieces. Howdo you do it?

    A: Try figuring this one out on your own as a hint well tell you that youll have to think out of the box on this one.

    The answer: stack the pieces

    The correct answer is to cut the cake in quarters (4 pieces) using 2 of the cuts, then stackall 4 of the pieces and then split all four of the stacked pieces with the third cut.

  • 8/10/2019 Puzzles as Programmer Interview Question

    3/32

    You might think of this as sort of a trick question, since moving the pieces issomething out of the box but somebody asking this question is probably lookingfor that kind of out of the box thinking.

    1. Given the numbers 1 to 1000, what is the minimum number [of] guesses needed tofind a specific number if you are given the hint higher or lower for each guess youmake?

    A: This may be considered a trick question, since its deceptively easy. Read the questioncarefully and youll note that the question asks for the minimum number of guesses. Thinkabout it someone can guess the right question on their first try right?

    So, the answer here would be 1, since it would take only one correct guess to find a specificnumber.

    Finding the max imum number of guesses

    But, what if we wanted to find the maximum number of guesses?

    Well, think about this one. What if the number that you have to guess is 1 and you startguessing from 1,000? Then, if the person who knows the number keeps saying lower, thenyou would guess 999,998,9976,5,4,3,2, and finally until you get to 1.

  • 8/10/2019 Puzzles as Programmer Interview Question

    4/32

    The maximum number of guesses

    This means that the maximum number of guesses is 999.

    But, you must be saying, thats a stupid answer because no one would take that approachto guessing unless they were really foolish.

    Using Binary Search

    The approach most programmers would take is dividing the set of numbers in half and thenworking accordingly basically a binary search as it is known in computer science.

    So, lets say the number you were trying to guess is a 1. Then, you would start from themiddle of 1,000 which is 500. The person giving you hints would keep saying lower andyou would end up with something like this sequence of numbers:

    500, 250, 125, 63, 32, 16, 8, 4, 2, 1

    Counting the number of guesses above would give you 10. Another way of arriving atthis estimate would be to take the log base 2 of 1,000 which would also give youapproximately 10. Since you cant possibly have a fraction of a guess, the result of logbase 2 of 1000 should be rounded up to a whole number. Log base 2 of 1000actually gives 9.965, but we round that up to 10 to get an integer.

  • 8/10/2019 Puzzles as Programmer Interview Question

    5/32

    1. Suppose that you are standing in a hallw ay nex t to 3 light switches, which are alloff. There is another room down the hallway, where there are 3 incandescent lightbulbs each light bulb is operated by one of the switches in the hallway. Becausethe light bulbs are in another room, you can not see them since you are standing inthe hallway.

    How would you figure out which switch operates which light bulb, if you can only gothe room with the light bulbs one time, and only one time?

    A: A lot of times it helps just to state the obvious, so lets do that here:

    If a switch is in the on position then the light bulb corresponding to that switch will light upas well.

    Now, given the previous information, can we solve this problem? Well, just think about thedifferent possibilities. If we have all switches on, then when we go into the attic all the lightbulbs will be on. This tell us nothing about which switch corresponds to which light bulb. If wehave 2 of the switches on, then when we go into the attic, 2 of the light bulbs will be on. Thiswill tell us that the switch which is in an off position corresponds to the light bulb thats off inthe attic. And if we have 1 switch on (and the other 2 off), then we know which switchcorresponds to the light bulb thats on.

    Neither of the last 2 solutions are complete because they only tell us about one of theswitches and the light bulb that it corresponds to. But the problem clearly states that weneed to know about all three of the switches, and which bulb each one corresponds to.

    Think outside the box

  • 8/10/2019 Puzzles as Programmer Interview Question

    6/32

    So its now obvious that we must find some other way of solving the problem. Lets thinkoutside the box: what else do we know here?

    Well, the properties of a switch dont seem to have anything unique to them they are justeither simply on or off.

    2. What happens to a light bulb when turned on?

    A: What about the light bulbs? Well, we do know that incandescent light bulbs get hot whenthey are on this sounds like it might just lead to something useful that can help us. Weknow that the longer an incandescent light bulb stays on the hotter it gets.

    So, if we turn one switch off after 5 minutes, turn the second one on, and leave the last one

    off then what happens? Well, the light bulb corresponding to the first switch will still be warm(even though its off), the bulb corresponding to the second switch will be on, and the bulbcorresponding to the last switch will be off. This is enough information so that we can go intothe attic only once and figure out which switch belongs to which light bulb.

    This question is tricky because it requires thinking somewhat creatively to come up with asolution. Measuring someones intelligence based on a problem like this is probably not agood idea whether people stumble open an answer to this is often due to their luck. Unless,of course, they already knew the answer.

  • 8/10/2019 Puzzles as Programmer Interview Question

    7/32

    1. You are standing in a school hallway lined with 100 closed lockers. You then openall 100 lockers. After this, you then close every 2nd locker (so the 2nd, 4th, 6th98thand 100th are all closed). Then, you go to every third locker and open it if it is closedor close it if it is open (lets call this toggling the locker for our discussion). Youproceed to toggle every nth locker on pass number n. So, for example, on passnumber 16 you will toggle every 16th locker. After your hundredth pass of thehallway, in w hich you toggle only locker number 100, how many lockers are nowopen?

    In a hall w ith x lockers, how many lockers remain open after pass number x?

    A: This problem can seem intimidating at first. Just drawing a diagram of 100 lockers andactually toggling each one would not really display any intelligence on your part. In fact, youdprobably look pretty dumb. Clearly, there must be a more intelligent way of solving this

    problem since you wouldnt be asked this question if there werent some smarter, moreefficient way of solving this.

    Start Out Small

    This isnt the type of problem that gets solved just by staring at it and hoping an answer justcomes to you. So, a good idea is to try some different numbers (much smaller than 100, of course), and see if that helps you notice some patterns.

    Lets just start by choosing a random locker, and lets determine whether it will end up open

  • 8/10/2019 Puzzles as Programmer Interview Question

    8/32

    or closed. Lets choose locker # 6.

    Lets go through each pass:

    Pass # 1: all lockers are opened, including locker # 6

    Pass # 2: all even numbered lockers are closed, including locker # 6

    Pass # 3: every 3rd locker is toggledso 3, 6, 9, .96, 99. includes locker # 6.

    Pass # 4: 4, 8, 12, etc. are all toggled. Excludes #6.

    Pass # 5: 5, 10, 15 are all toggled. Excludes # 6 again.

    Pass # 6: 6, 12, 18, etc. are all toggled. Includes # 6.

    Passes greater than 6: Locker #6 will not be toggled again, since those will all start farther down the hall.

    So, one thing you may notice after running through this example is that locker #6 is onlytoggled when the number of the pass (also called x) that you are on is a factor of the # 6 you can see that 1,2, 3, and 6 are all factors of 6. And those are all the passes on which thelocker 6 is toggled the sequence is open, close, open and then close. So, locker # 6 endsup closed.

    Now that sounds like promising information. Since we are dealing with factors here, why nottry a prime number since a prime number only has 2 factors itself and 1. Lets try thenumber 13 as our prime number. The factors are 1 and 13 which means that the operationsare open and then close for any pass greater or equal to 13. So, it ends up being closed.

  • 8/10/2019 Puzzles as Programmer Interview Question

    9/32

    So, we know one thing for sure: that the lockers are only toggled on passes that are factorsof the individual locker number.

    Its all about the factors

    What else do we know? Well, we know that lockers are closed every 2nd, 4th, 6th, etc. timesthey are toggled so if a locker is toggled an even number of times it ends up closed,otherwise it ends up open. Combining this knowledge with our other knowledge, we can saythat a locker will end up closed if it has an even number of factors, because the number of times a locker is toggled equals the number of factors. If a locker has an odd # of factors, thelocker will end up being open.

    So, this knowledge makes a big difference weve really simplied the problem now all weneed to do is figure out how many numbers between 1 and 100 have an odd # of factors, andwe will have our answer!

    First, lets jump into the math to find our answer.

    What exactly does it mean when we say that c is a factor of d, for some #s c and d?

    Well, it means that c multiplied by some other number b is equal to d. This also means that bis also a factor of d since multiplication is commutative (c*b = b*c).

    So, the number of factors is usually even since factors tend to come in pairs. And an evennumber of factors means that the locker will end up being closed.

    But, what if the factors are the same numbers if b is equal to c? Then, the number d wouldbe a perfect square, since b*b would equal d, which would be a perfect square. This would

  • 8/10/2019 Puzzles as Programmer Interview Question

    10/32

    also mean that we would have an odd number of factors and an odd number of factors wouldmean that particular locker will remain open.

    Since there arent very many perfect squares between 1 and 100, you can list them out here they are: 1, 4, 9, 16, 25, 36, 49, 64, 81 and 100 so, exactly 10 lockers will remainopen.

    Generalizing the solution

    Now, lets generalize the solution so that we have an answer to the original question:

    If there are x lockers, the number of open lockers will be the number of perfectsquares between 1 and x (inclusive). To count them, all you need to do is find thesquare root of the largest perfect square less than or equal to x. So, the generalsolution would be: floor(sqrt(x))

    Heres an example to help illustrate this:

    If x = 200, then sqrt(200) = 14.142 ..

    And because floor(sqrt(200)) = 14,

    there will be 14 open lockers.

    In an interview situation you may not have been able to easily get to the answer here. But, the

  • 8/10/2019 Puzzles as Programmer Interview Question

    11/32

    most important thing is that you give it your best shot, and you approach it intelligently!

    Here, you should also see the value of breaking the problem down into a more manageablesize, finding a pattern, and then coming up with a solution.

    1. You are given 8 pennies, 7 of which weigh exactly the same, but one pennyweighs less than the other 7. You also have a judge scale. Find the one penny thatweighs the least in less than 3 steps.

    A: Like most puzzles, the answer to this question is not going to just jump out to you youllhave to break the question down.

    We know that a judge scale is used to compare the weights of different things and it tells us if something on one side is heavier, lighter, or the same weight as whatever is on the other sideof the scale.

    Since the question asks us to find the penny that weighs the least in less than 3 steps, thatmeans that we will need to find the lightest penny in either 1 or 2 steps.

  • 8/10/2019 Puzzles as Programmer Interview Question

    12/32

    So, lets think of finding the answer to this in 1 step: Really, the only thing we can do iscompare 2 pennies on the scale and if one is lighter than the other than we know that it is thelightest. But, we would have to be lucky enough to pick the lightest penny to compare, andtheres no way we can do this without just getting plain lucky. So, theres also no way we cando this in 1 step.

    Has to be done in 2 steps

    Now, we have to be able to do this in 2 steps so how is this possible? What if we just trieddividing the 8 pennies into groups? Lets create 2 groups of 3 pennies each, and 1 group of 2pennies.

    Now, lets put the 2 groups of 3 pennies on the scale. What do we know if they are equal?Well, if theyre equal then the lightest penny must be in the group of 2!

    Then, lets take 2 pennies in the group of 2 and compare them to each other. They cantpossibly be equal, so we know that the lighter one is our lightest penny! Bravo we have ananswer in 2 steps.

    But, what if when comparing the 2 groups of 3 pennies, it didnt turn out that they were equal?Well, one of the groups of 3 would be lighter, which means that the lighter penny would be inthat group.

    So, we could take 2 pennies out of that group of 3 and compare them. If theyre equal weknow the 3rd penny is the lightest. If one is lighter than the other, then we know that penny isthe lightest. So, again the problem is solved in 2 steps.

  • 8/10/2019 Puzzles as Programmer Interview Question

    13/32

    Summary of the solution

    Heres a summary of the solution:

    First, you split the 8 pennies into 3 groups of pennies 2 groups with 3 pennies each and 1group with 2 pennies. Then, you compare the weight of the first two groups of 3 pennies eachby putting them on the scale.

    Scenario #1: The 2 groups weigh the same. This means the lightest coin is in the group of 2.So, take those 2 pennies and compare them to each other and find the lightest coin.

    Scenario #2: The 1st group weighs more than the 2nd group. Take group #2 (3pennies) and pick any 2 pennies out of that group of 3. If they weight the same, thenthe third penny is lighter. If they dont weigh the same then the lighter one is obviouslythe lightest penny.

    1. How would you we igh an e lephant without using a scale?

  • 8/10/2019 Puzzles as Programmer Interview Question

    14/32

    A: If we are not allowed to use a scale then what are our options? Well, there arent verymany.

    It helps to think of natural things of which we already know the properties of. Lets think of water thats something natural that has a lot of measurable properties.

    2. What could we do with water?

    A: Well, we do know that one of the properties of water is that it weighs 1 kg. per liter, or 8.34

    lbs per gallon. How can we use this knowledge to our advantage to solve this problem?

    3. Put the elephant in a tank

    A: What if we put the elephant on a big raft inside a full tank of water?

    What we could do then is measure the volume of water thats displaced and that would giveus a great approximation of the weight of the elephant.

    So, say that 7500 liters (or about 1,900 gallons) of water is displaced, then thatmeans the elephant weighs about 7500 kilograms, or 16,500 pounds.

  • 8/10/2019 Puzzles as Programmer Interview Question

    15/32

    1. Suppose you have a 3 liter jug and a 5 liter jug (this could also be in gallons). The jugs have no measurement lines on them either. How could you measure exactly 4

    liter using only those jugs and as much extra water as you need?

    A: This question was actually asked in a popular movie Die Hard With A Vengeance.

    Try figuring this one out on your own before reading the answer. It should be clear that thegoal is to end up with 4 liter in the 5 liter jug, since having 4 liters in the 3 liter jug isimpossible.

    The answer, broken down in steps:

    Step 1 : First, fill the 5 liter jug and then pour it into the 3 liter jug. The 5 liter jug now has only2 liters left.

    Step 2 : Next, empty out the 3 liter jug. Then, pour the 2 liters from the 5 liter jug to the 3 liter jug. So, now the 3 liter jug has 2 liters.

    Step 3 : Fill the 5 liter jug again, and pour 1 liter into the 3 liter jug. Now, whats left in the 5liter jug? Well, exactly 4 liters! Theres your answer.

    7

    http://wibiya.conduit.com/?campaign_id=100
  • 8/10/2019 Puzzles as Programmer Interview Question

    16/32

    Quick Links

    ViewTranscriptsTestimonialsView blogsTop Scorers

    1. Lets say that you have 25 horses, and you want to pick the fastest 3 horses out of those 25. In each race, only 5 horses can run at the same time. What is the minimumnumber of races required to find the 3 fastest horses without using a stopwatch?

    A: This question is an interesting one. Lets start by asking ourselves, what are our limitations? Well, we dont have a stopwatch so we cant time the horses. That means thatwe cant compare the race times of each horse otherwise we could simply have 5 races of 5horse each and pick out the 3 fastest t imes to get the 3 fastest horses. Remember that wecan only race 5 horses in each race (otherwise we could just have one race with 25 horses,pick the 3 fastest, and we would be done!).

    Draw out a table

    In problems like this, it helps tremendously to create some sort of visual aid that you canrefer to. With that in mind, we have created this table where each entry represents a differenthorse.

    X1 X2 X3 X4 X5

    X6 X7 X8 X9 X10

    X11 X12 X13 X14 X15

    X16 X17 X18 X19 X20

    X21 X22 X23 X24 X25

    http://www.brainbeez.com/index.php/index.php?option=com_lms&task=topscorer&Itemid=100http://www.brainbeez.com/index.php/index.php?option=com_content&view=section&layout=blog&id=5&Itemid=106http://www.brainbeez.com/index.php/index.php?option=com_phocaguestbook&view=phocaguestbook&id=1&Itemid=101http://www.brainbeez.com/index.php/index.php?option=com_lms&task=tests&Itemid=91
  • 8/10/2019 Puzzles as Programmer Interview Question

    17/32

    Lets say that we have 5 races of 5 horses each, so each row in the table above represents arace. So, X1 X2 X3 X4 X5 represents a race, and X6 X7 X8 X9 X10 represents another race, etc. In each row, the fastest horses are listed in descending order, from the fastest(extreme left) to the slowest (extreme right). The fastest horses in each race are the ones onthe left so in the first race X1 was the fastest and X5 was the slowest. In the second raceX6 was the fastest, X7 was the second fastest and so on.

    Only 5 horses each race

    So, now we ask ourselves: what do we know after these 5 races? Well, we do have the 5 fivefastest horses from each race (X1, X6, X11, X16, and X21). But, does that mean we have the5 fastest horses? Think about that for a second. Well, actually it does not mean that we havethe 5 fastest horses. Because, what if the 5 fastest horses just happened to be in the firstrace so X1 X2 X3 X4 X5 are the fastest horses. X1, X6, X11, X16, and X21 are all the fastesthorses in their individual groups, but there could be one group that just happened to have allof the fastest horses. Remember we havent compared all the horses to each other since wecan only run 5 horses in a race, so that is still a possibility. This is very important tounderstand in this problem.

    2. Work through a process of elimination

    A: Well, now that weve had 5 different races, we can eliminate the slowest 2 horses in eachgroup since those horses are definitely not in the top 3. This would leave these horses:

    X1 X2 X3

    X6 X7 X8

    X11 X12 X13

  • 8/10/2019 Puzzles as Programmer Interview Question

    18/32

    X16 X17 X18

    X21 X22 X23

    We also know the 5 fastest horses from each group but its important to remember that the5 group leaders are not necessarily the 5 fastest horses. So what can we do with thatinformation?

    Well, we can race those 5 horses against each other (X1, X6, X11, X16, and X21) and thatwould be the 6th race. Lets say that the 3 fastest in that group are X1, X6, and X11 automatically we can eliminate X16 and X21 since those 2 are definitely not in the top 3.

    What other horses can we eliminate after this 6th race? Well, we can automatically eliminateall the horses that X16 and X21 competed against in the preliminary races since X16 andX21 are not in the top 3 then we also know that any horse thats slower than those 2 isdefinitely not in the top 3 either. This means we can eliminate X17 X18 X22 and X23 alongwith X16 and X21.

    Now, we also know that X1 is the fastest horse in the group since he was the fastesthorse out of the 5 group leaders. So, we dont need to race X1 anymore. Are thereany other horses that we can eliminate from further races? Well, actually there are.Think about it if X6 and X11 are the 2nd and 3rd fastest in the group leaders, thenwe should be able to eliminate X8 since X6 raced against him and he was in 3rd placein that race. X7 could only possibly be the 3rd fastest, and since X8 is slower than X7,we can safely eliminate X8. We can also eliminate X12 and X13 since X11 was the 3rdfastest in the group leaders, and X12 and X13 were slower than X11.

    So, all together we can eliminate these horses after the 6th race: X17 X18 X22 X23 X16 X21,X12, X13, X8 and X1. This leaves us with the following horses to determine the 2nd and 3rdfastest horses:

    X2 X3

    X6 X7

    X11

  • 8/10/2019 Puzzles as Programmer Interview Question

    19/32

    3. What is the solution?

    A: This means we only have 5 horses left! Now we race those horses one more time in theseventh (7th) race and we can take out the top 2 horses and that would mean we have the2nd and 3rd place horses! So, we have found our answer! It takes 7 races to find the top 3horses in this problem.

    1. Theres an activity in America, with one-on-one contests, and a nationalchampionship. The same person won the championship on two different occasionsabout 65 years apart. Name the activity.

    A: This was a question that Charlie Munger (a partner of Warren Buffets) asked a group of students in a speech he gave. This is a fun question to try to answer, because it really is justa test to see your analytical skills. Try to figure this out on your own were sure that youcan figure out the answer on your own if you think carefully.

  • 8/10/2019 Puzzles as Programmer Interview Question

    20/32

    Breaking the problem down

    Lets figure this out. Well, since the championship was won by the same person over a span

    of 65 years, that means the second time around this person must have won thechampionship when he was at least 85 years old, and thats assuming that he first won thechampionship when he was at least 20 years old.

    This also means that theres no way that this activity requires any hand eye coordination because anyone over 85 years old will simply not be able to beat someone in his 20s. So,theres no way this was a billiards tournament, and definitely not a tennis tournament.

    What about a chess tournament? Well, even though it is a mental game, it does require quitea bit of stamina and is far too competitive for an 85 year old to be competitive at a nationallevel, even if he can beat a lot of other younger people. If not chess, then what aboutcheckers? Well, that sounds like a potential answer! Because, that is definitely a gamewhere experience can make you the best even though you may be 85 years old. And its alsoa game which does not require much stamina, or any athleticism at all. And that is thecorrect answer it is a checkers tournament.

    The name of this person, in case you were wondering is Asa A. Long (born on 20 Aug 1904).He became

    the youngest US national champion, at 18 years old 64 days, when he won in Boston,

    Massachusetts, USA on 23 Oct 1922. He then became the oldest person to win the nationalcheckers tournament in America, at age 79 years 334 days

    when he won his sixth title in Tupelo, Mississippi, USA on 21 Jul 1984.

  • 8/10/2019 Puzzles as Programmer Interview Question

    21/32

    1. Four people need to cross a rickety rope bridge to get back to their camp at night.Unfortunately, they only have one flashlight and it only has enough light left for seventeen minutes. The bridge is too dangerous to cross without a flashlight, and itsonly strong enough to support two people at any given time.

    Each of the campers walks at a different speed. One can cross the bridge in 1minute, another in 2 minutes, the third in 5 minutes, and the slowest camper takes 10minutes to cross. How can the campers make it across in exactly 17 minutes?

    A: Lets start out trying to solve this problem by re-stating some of the facts that we have:only 2 travelers can cross the bridge at a time, there is only one flashlight, each traveler has adifferent speed, and our goal is to have all travelers cross the bridge in exactly 17 minutes. Italways helps to restate the relevant facts so that we can narrow down the parameters of theproblem.

    So, its clear that there will need to be someone who brings back the flashlight every time.

    This means that it makes the most sense to have 2 people cross the bridge at a time, butonly have one person bring back the flashlight. And, why not just have the fastest personbring back the flashlight every time the 1 minute person? Heres what it would look like if each line represents a separate trip, and if we just use their time in numbers to refer to thecampers:

  • 8/10/2019 Puzzles as Programmer Interview Question

    22/32

    Trip Elapsed Time

    1 5 10 ------------- 2 1 -------------------> 2

    2 5 10 2 5

    4 10 10

    Total Elapsed time: 19 minutes

    We were only able to get all of the campers over the bridge in 19 minutes. But, the questionclearly asks us how to do it in 17 minutes. How can we improve? Well, lets ask ourselvesthis question: what assumptions did we make that we can improve on? We did assume thathaving the 1 minute camper come back every time is the best way to do this. That soundslike there may be some room for improving that assumption, and, in fact, there is: it seemslike having 10 and 1 cross the bridge at the same time is a waste, because anyone whocrosses the bridge with 10 will have to take 10 minutes since 10 will slow them down. Whynot have instead have 10 go with the second slowest camper, 5, and then have 2 come back?So, it would look like this:

    Trip Elapsed Time

  • 8/10/2019 Puzzles as Programmer Interview Question

    23/32

    1 5 10 ------------- 2 1 -------------------> 2

    2 5 10 2 10

    4 1 5 10 2

    Total Elapsed time: 17 minutes

    And, voila! We have our answer as shown above!

    1. Suppose you have 8 marbles and a two-pan balance used to compare the w eightof 2 things. All of the marbles weigh the same except for one, which is heavier thanall of the others. How would you find the heaviest marble if you are only allow ed toweigh the marbles 2 times?

  • 8/10/2019 Puzzles as Programmer Interview Question

    24/32

  • 8/10/2019 Puzzles as Programmer Interview Question

    25/32

  • 8/10/2019 Puzzles as Programmer Interview Question

    26/32

    Is there a way we can take this information and generalize it into a solution for n marbles?What if we started out with 9 marbles? Well, we could find the heaviest marble with just 2weighings as well because in the first weighing we could eliminate 2/3 or 6 of the marbles,and in the second weighing we could eliminate 2/3 of the remaining 3 marbles or 2 marbles,which would leave us with just the heaviest marble. It sounds like with every weighing wewould be left with 1/3 of the marbles, and once we get to just one marble, then we are donesince we have found the heaviest marble.

    Come up with an equation

    Another way of saying this is that, if x is the number of weighings and n is the number of marbles, then 3x would equal n. This is true in the scenario where we have 9 marbles,because 32 is equal to 9 so it takes 2 weighings to find the heavy marble out of 9 marbles.So, if x is the minimum number of weighings to find the heavy marble, then this would meanthat x = log3n.

    What if we just had 8 marbles would that equation (x = log3n) still apply? Actually, it wouldnot because of the fact that the the log38 is equal to 1.893 and we can not have 1.893weighings we can only have whole numbers. So, what needs to be done here? Well, wewould need to round up to 2. But, the real question is whether rounding up would be valid for all other number of marbles.

    2. Does rounding up always work?

  • 8/10/2019 Puzzles as Programmer Interview Question

    27/32

    A: Lets see if rounding up works for 10 marbles. log310 would give us a little more than 2 so if we round up to 3 would that be valid? In other words, in only 3 weighings can we find theheavy marble out of 10 marbles? Lets break it down we would start out with 2 groups of 3(which we put on the scale) and one group of 4. Then, after one weighing we would be left witheither 3 marbles or 4 marbles, depending on which group the heavy marble is in. If its in thegroup of 3 marbles we would need one more weighing to find the heavy marble, for a total of 2weighings. However, if the heavy marble is in the group of 4, then we would need 2 moreweighings for a total of 3 weighings. So, the answer varies depending on where the marble is.But, the question asks us to find the minimum number of weighings in which we areguaranteed to find the heavy marble, and that would be 3 weighings in this case.

    So, we would have to round up in every case we have a fractional number in order to find theheaviest marble, and another way of saying that is we want to find the ceiling of log3n. Andthat is the correct answer given n marbles, it takes ceiling (log3n) weighings to find theheavy marble.

    On e interesting thing to note here is how much easier it would have been to solve theprevious problem if the number of marbles was 9, because you are able to split themarbles into 3 groups of 3. But, with 8 marbles, the problem becomes more difficultand forces you to think a bit more. So, its always good to remember to try outdifferent scenarios, and not to be misled into thinking about the problem in the wrongway because of a small detail in the problem.

    1. You have 2 fuses and a lighter. When each fuse is lit, it takes exactly one hour toburn from one end to the other. You can not make any other assumptions about thefuses they are not identical, they do not burn at a constant rate, and their length

  • 8/10/2019 Puzzles as Programmer Interview Question

    28/32

    has nothing to do with the time remaining (so just because half of the fuse is leftdoes not mean that half an hour is left before the fuse is finished).

    Using only those 2 fuses and the lighter, how would you measure a period of exactly45 minutes?

    A: When you first think about this puzzle (it may even be called a riddle), it may seem

    impossible to find a solution. But, there must be an answer since someone is asking thisquestion. What do we have and what do we know? We have 2 fuses, a lighter, and we knoweach fuse takes one hour to burn. It seems like all we can do is measure one hour, since wecant make any other assumptions. So, this means we must really try to think creatively,because there is something here that we are missing.

    Thinking outside the box

    What if we start the flame from the middle of a fuse? Would that allow us to measure half anhour? Actually, no it would not, because we can not assume that half of the length of the fusewould burn in half an hour it may just be that 1/10th of the length of the fuse take 50minutes to burn and the other 9/10ths of the fuse takes 10 minutes to burn. So, starting theflame in the middle of the fuse is not a valid option.

    What would happen if we burn a fuse from both sides? That sounds interesting. Well, what

  • 8/10/2019 Puzzles as Programmer Interview Question

    29/32

    would happen is that the fuse would burn out in just 30 minutes this is because the fusewould be burning from both sides and the flames would burn until they meet each other andextinguish after exactly 30 minutes. Well, that sounds very useful because we can nowmeasure 30 minutes!

    Now that we can measure 30 minutes, how could we measure 15 minutes more to get45 minutes total? Well, can we use the idea of burning a fuse from both sides tomeasure that extra 15 minutes? That sounds like it has potential what if we burnfuse # 1 from both ends, and we burn fuse #2 from only one end. Then, after 30minutes has passed, we can burn the other end of fuse #2. Fuse #2 would finishburning in 15 minutes because it has already has 30 minutes worth of time burnedfrom it, but it is also burning from both ends so that cuts the burning time in half.

    And 30 + 15 would give us 45 minutes so we finally have an answer!

    1. Suppose 2 boys are walking in the woods and they decide to take a shortcutthrough a rai lroad tunnel. They had walked 2/3 of the w ay through the tunnel, butthen something horrible happened: a train was coming in the opposite direction, andit was coming close to the entrance of the tunnel. Each boy ran in a differentdirection to get out of the tunnel and avoid the incoming train. Each boy ran at thesame exact speed of 10 miles per hour, and each boy managed to escape the trainat the exact instant in which the train would have hit and killed him. If the train wasmoving at a constant speed and each boy was capable of instantaneousacceleration, then how fast was the train going?

    A: Take a look at the diagram below to visualize the problem. Let A and B be the 2 boys, and

  • 8/10/2019 Puzzles as Programmer Interview Question

    30/32

    you can see that they are both running in opposite directions. You can see that they boyshad walked 2/3 of the way through the tunnel, and you can also see the train approaching.

    ||

  • 8/10/2019 Puzzles as Programmer Interview Question

    31/32

    -------------------------

  • 8/10/2019 Puzzles as Programmer Interview Question

    32/32

    < Prev

    http://www.brainbeez.com/index.php/index.php?option=com_content&view=article&catid=234%3Aprogammer-interview-questiontype&id=1837%3Ageneralmiscellaneous-programmer-interview-question&Itemid=59