tag:blogger.com,1999:blog-6850709577328066952017-05-15T13:59:46.398-07:00Google Interview QuestionsAdministratornoreply@blogger.comBlogger16125tag:blogger.com,1999:blog-685070957732806695.post-5947933902682748862011-10-15T04:59:00.001-07:002011-10-15T04:59:07.363-07:00Classify the Hashing Functions based on the various methods by which the key value is foundAdministratornoreply@blogger.com0tag:blogger.com,1999:blog-685070957732806695.post-60154091616279468012011-09-30T10:14:00.001-07:002011-09-30T10:14:50.875-07:00Common multipleQ: Given two numbers m and n, write a method to return the first number r that is<br />divisible by both (e.g., the least common multiple).<br /><br />A:<br />The Approach:<br />What does it mean for r to be divisible by m and n? It means that all the primes in m must go into r, and all primes in n must be in r. What if m and n have primes in common?<br /><br />For example, if m is divisible by 3^5 and n is divisible by 3^7, what does this mean about r? It means r must be divisible by 3^7.<br /><br />The Rule: For each prime p such that p^a \ m (e.g., m is divisible by p^a) and p^b \ n, r must be divisible by p^max(a, b)<br /><br />The Algorithm:<br />Define q to be 1.<br />for each prime number p less than m and n:<br />find the largest a and b such that p^a \ m and p^b \ n<br />let q = q * p^max(a, b)<br />return qAdministratornoreply@blogger.com6tag:blogger.com,1999:blog-685070957732806695.post-19779691805535762932010-06-30T06:12:00.000-07:002010-06-30T06:12:28.084-07:00Card Pairology?John and Matt decide to play a game using a deck of cards. The game involves flipping two cards at a time. If both cards are black, they go into John’s pile and if both cards are red, they go into Matt’s pile. If one card is red and one card is black, they go into a discard pile. The game is played until all 52 cards have been flipped. The person with the most cards in their pile wins. If there is a tie, John wins. If Matt has more cards than John, then John pays Matt a dollar. What is the chance of Matt getting a dollar?<br /><br /><b>Ans</b><br /><br />Any guesses? The chance of Matt getting any money is zero. Do you know why?<br /><br />Say the deck is arranged in a way such that there are 13 black pairs. In this situation, John gets 13 black pairs. And since there are no other black cards left, Matt gets 13 red pairs too. So there’s a tie and John wins.<br /><br />Suppose there are 12 black pairs in the deck. In this case, there would be 2 black cards left in the deck that would pair up with 2 other red cards in the deck. These 2 black cards would never be together since we have already claimed that there are only 12 black pairs in the deck. As a result, 2 pairs of cards end up in the discard pile. The remaining cards would all be red and Matt would get 12 red pairs too. So once again, there’s a tie and John wins.<br /><br />We could continue this process through induction. Assuming there are 11 black pairs, there would be 4 other black cards that pair up with 4 other red cards and go into the discard pile. Once again, both John and Matt end up with 11 pairs of cards each and John wins.<br /><br />Here’s a table to explain each case.<br />John Discarded Matt<br />13 pairs 0 pairs 13 pairs<br />12 pairs 2 pairs 12 pairs<br />11 pairs 4 pairs 11 pairs<br />10 pairs 6 pairs 10 pairs<br />9 pairs 8 pairs 9 pairs<br />8 pairs 10 pairs 8 pairs<br />7 pairs 12 pairs 7 pairs<br />6 pairs 14 pairs 6 pairs<br />5 pairs 16 pairs 5 pairs<br />4 pairs 18 pairs 4 pairs<br />3 pairs 20 pairs 3 pairs<br />2 pairs 22 pairs 2 pairs<br />1 pair 24 pairs 1 pair<br />0 pairs 26 pairs 0 pairs<br /><br />So we see that in any case, there is a tie between John and Matt. So Matt never ends up with a dollar.Administratornoreply@blogger.com0tag:blogger.com,1999:blog-685070957732806695.post-27671852407650566392010-05-18T11:29:00.000-07:002010-05-18T11:29:36.224-07:00One gold barYou have someone working for you for seven days and you have one gold bar to pay them. The gold bar is segmented into seven connected pieces. You must give them a piece of gold at the end of every day. If you are only allowed to make two breaks in the gold bar, how do you pay your worker?<br /><br /><b>Ans</b><br /><br /> As 7 = 1 + 2 + 4, this implies there are only 2 divisions (cuts).<br /><br />Day 1: Give [1] and you have [2+4]<br /><br />Day 2: Give [2] and take back [1]. You have [1+4]<br /><br />Day 3: Give [1+2] and you have [4]<br /><br />Day 4: Give [4] and take back [1+2]. You have [1+2].<br /><br />Day 5: Give [1+4] and you have [2]<br /><br />Day 6: Give [2+4] and take back [1]. You have [1]<br /><br />Day 7: Give [1+2+4] and you have nothing!<br /><br />The same trick can be applied to:<br /><br />15 Days = 1 + 2 + 4 + 8<br /><br />31 Days = 1 + 2 + 4 + 8 + 16<br /><br />And so on.<br /><br />The main point of division is:<br /><br />1 = 2^0<br /><br />2 = 2^1<br /><br />4 = 2^2<br /><br />8 = 2^3<br /><br />16= 2^4<br /><br />And so on...Administratornoreply@blogger.com3tag:blogger.com,1999:blog-685070957732806695.post-17443352661300740382010-05-02T13:25:00.001-07:002010-05-02T13:25:34.270-07:00How Old Are My Children?<b>Question:</b> Two old friends, Jack and Bill, meet after a long time.<br /><br />Three kids<br />Jack: Hey, how are you man?<br />Bill: Not bad, got married and I have three kids now.<br />Jack: That’s awesome. How old are they?<br />Bill: The product of their ages is 72 and the sum of their ages is the same as your birth date.<br />Jack: Cool… But I still don’t know.<br />Bill: My eldest kid just started taking piano lessons.<br />Jack: Oh now I get it.<br /><br />How old are Bill’s kids?<br /><br /><b>Answer:</b> Let’s break it down. The product of their ages is 72. So what are the possible choices?<br /><br />2, 2, 18 – sum(2, 2, 18) = 22<br />2, 4, 9 – sum(2, 4, 9) = 15<br />2, 6, 6 – sum(2, 6, 6) = 14<br />2, 3, 12 – sum(2, 3, 12) = 17<br />3, 4, 6 – sum(3, 4, 6) = 13<br />3, 3, 8 – sum(3, 3, 8 ) = 14<br />1, 8, 9 – sum(1,8,9) = 18<br />1, 3, 24 – sum(1, 3, 24) = 28<br />1, 4, 18 – sum(1, 4, 18) = 23<br />1, 2, 36 – sum(1, 2, 36) = 39<br />1, 6, 12 – sum(1, 6, 12) = 19<br /><br />The sum of their ages is the same as your birth date. That could be anything from 1 to 31 but the fact that Jack was unable to find out the ages, it means there are two or more combinations with the same sum. From the choices above, only two of them are possible now.<br /><br />2, 6, 6 – sum(2, 6, 6) = 14<br />3, 3, 8 – sum(3, 3, 8 ) = 14<br /><br />Since the eldest kid is taking piano lessons, we can eliminate combination 1 since there are two eldest ones. The answer is 3, 3 and 8.Administratornoreply@blogger.com0tag:blogger.com,1999:blog-685070957732806695.post-89563973020175559032010-05-02T13:24:00.002-07:002010-05-02T13:24:53.918-07:00Piece of Cake<b>Question:</b> How would you cut a rectangular cake into two equal pieces when a rectangular piece has already been cut out of it? The cut piece can be of any size and orientation. You are only allowed to make one straight cut.<br /><br /><b>Answer:</b> Simple question right? There are two possible solutions to this problem. People often overlook the easier solution to this problem. Let’s start with the easiest solution. If you make one straight horizontal cut along the height of the cake, the resulting slices are of equal sizes. But this solution may not work so well on a cake with icing. So let’s rethink.<br /><br />In general, when a straight cut is made at any angle through the center of a rectangle, the resulting pieces are always of equal area. So let’s consider our situation. What if we make a straight cut such that it passes through the center of both the rectangles? Since the cut halves both the rectangles, the resulting two pieces are guaranteed to have equal area. Each piece has an area equal to half the original cake minus half the area of the missing rectangular piece. This results in two pieces of equal size, assuming the cake’s height is same at all points.Administratornoreply@blogger.com5tag:blogger.com,1999:blog-685070957732806695.post-11842772611370074032010-05-02T13:24:00.000-07:002010-05-02T13:24:00.220-07:00Pick a Random Byte<b>Question:</b> You have a stream of bytes from which you can read one byte at a time. You only have enough space to store one byte. After processing those bytes, you have to return a random byte. Note: The probability of picking any one of those bytes should be equal.<br /><b><br />Answer:</b> Since we can only store one byte at a time, we have to be able to work with the bytes as they come in. We can’t store everything, count the number of bytes and pick one. That would be too easy wouldn’t it?<br /><br />Let’s think it through. We don’t know how many bytes there are, so at any point in time we should have a random byte picked from all the bytes we have seen so far. We shouldn’t worry about the total number of bytes at any time.<br /><br />When we get the first byte, it is simple. There is only one so we store it. When we get the second one, it has 1/2 probability of being picked and the one we have stored has a 1/2 probability of being picked (we can use any programming language’s built-in random function). After picking one, we replace the current stored byte with the one we picked. Now it gets interesting. When we get the third one, it has 1/3 probability of being picked and the one we have stored has a 2/3 probability of being picked. We pick one of the two bytes based on this probability. We can keep doing this forever.<br /><br />Just generalizing, when we get the nth byte, it has a 1/n probability of being picked and the byte we have stored has (n-1)/n probability of being picked.<br /><br />Sometimes, this question is a little confusing. The question said you have only space to store one byte but there is an assumption that you have space to store variables to track the number of bytes and so on. Another assumption is that the stream of bytes is not infinite. If not, we won’t be able to calculate (n-1)/n.Administratornoreply@blogger.com0tag:blogger.com,1999:blog-685070957732806695.post-17354275266764674812010-05-02T13:22:00.003-07:002010-05-02T13:22:54.191-07:00Probability of a Car Passing By<b>Question:</b> The probability of a car passing a certain intersection in a 20 minute windows is 0.9. What is the probability of a car passing the intersection in a 5 minute window? (Assuming a constant probability throughout)<br /><br /><b>Answer:</b> This is one of the basic probability question asked in a software interview. Let’s start by creating an equation. Let x be the probability of a car passing the intersection in a 5 minute window.<br /><br />Probability of a car passing in a 20 minute window = 1 – (probability of no car passing in a 20 minute window)<br />Probability of a car passing in a 20 minute window = 1 – (1 – probability of a car passing in a 5 minute window)^4<br />0.9 = 1 – (1 – x)^4<br />(1 – x)^4 = 0.1<br />1 – x = 10^(-0.25)<br />x = 1 – 10^(-0.25) = 0.4377Administratornoreply@blogger.com2tag:blogger.com,1999:blog-685070957732806695.post-85653642596671957572010-05-02T13:22:00.001-07:002010-05-02T13:22:17.325-07:00Boys and Girls<b>Question:</b> In a country where everyone wants a boy, each family continues having babies till they have a boy. After some time, what is the proportion of boys to girls in the country? (Assuming probability of having a boy or a girl is the same)<br /><br /><b>Answer:</b> This is a very simple probability question in a software interview. This question might be a little old to be ever asked again but it is a good warm up.<br /><br />Assume there are C number of couples so there would be C boys. The number of girls can be calculated by the following method.<br /><br />Number of girls = 0*(Probability of 0 girls) + 1*(Probability of 1 girl) + 2*(Probability of 2 girls) + …<br />Number of girls = 0*(C*1/2) + 1*(C*1/2*1/2) + 2*(C*1/2*1/2*1/2) + …<br />Number of girls = 0 + C/4 + 2*C/8 + 3*C/16 + …<br />Number of girls = C<br />(using mathematical formulas; it becomes apparent if you just sum up the first 4-5 terms)<br /><br />The proportion of boys to girls is 1 : 1.Administratornoreply@blogger.com0tag:blogger.com,1999:blog-685070957732806695.post-77377854304929406622010-05-02T13:21:00.001-07:002010-05-02T13:21:44.855-07:00Four People on a Rickety Bridge<b>Question:</b> Four people need to cross a rickety bridge at night. Unfortunately, they have only one torch and the bridge is too dangerous to cross without one. The bridge is only strong enough to support two people at a time. Not all people take the same time to cross the bridge. Times for each person: 1 min, 2 mins, 7 mins and 10 mins. What is the shortest time needed for all four of them to cross the bridge?<br /><b><br />Answer:</b> The initial solution most people will think of is to use the fastest person as an usher to guide everyone across. How long would that take? 10 + 1 + 7 + 1 + 2 = 21 mins. Is that it? No. That would make this question too simple even as a warm up question.<br /><br />Let’s brainstorm a little further. To reduce the amount of time, we should find a way for 10 and 7 to go together. If they cross together, then we need one of them to come back to get the others. That would not be ideal. How do we get around that? Maybe we can have 1 waiting on the other side to bring the torch back. Ahaa, we are getting closer. The fastest way to get 1 across and be back is to use 2 to usher 1 across. So let’s put all this together.<br /><br />1 and 2 go cross<br />2 comes back<br />7 and 10 go across<br />1 comes back<br />1 and 2 go across (done)<br /><br />Total time = 2 + 2 + 10 + 1 + 2 = 17 minsAdministratornoreply@blogger.com0tag:blogger.com,1999:blog-685070957732806695.post-6485384340129761932010-05-02T13:20:00.001-07:002010-05-02T13:20:50.567-07:00How Strong is an Egg?<b>Question:</b> You have two identical eggs. Standing in front of a 100 floor building, you wonder what is the maximum number of floors from which the egg can be dropped without breaking it. What is the minimum number of tries needed to find out the solution?<br /><br /><b>Answer:</b> The easiest way to do this would be to start from the first floor and drop the egg. If it doesn’t break, move on to the next floor. If it does break, then we know the maximum floor the egg will survive is 0. If we continue this process, we will easily find out the maximum floors the egg will survive with just one egg. So the maximum number of tries is 100 that is when the egg survives even at the 100th floor.<br /><br />Can we do better? Of course we can. Let’s start at the second floor. If the egg breaks, then we can use the second egg to go back to the first floor and try again. If it does not break, then we can go ahead and try on the 4th floor (in multiples of 2). If it ever breaks, say at floor x, then we know it survived floor x-2. That leaves us with just floor x-1 to try with the second egg. So what is the maximum number of tries possible? It occurs when the egg survives 98 or 99 floors. It will take 50 tries to reach floor 100 and one more egg to try on the 99th floor so the total is 51 tries. Wow, that is almost half of what we had last time.<br /><br />Can we do even better? Yes we can (Bob, the builder). What if we try at intervals of 3? Applying the same logic as the previous case, we need a max of 35 tries to find out the information (33 tries to reach 99th floor and 2 more on 97th and 98th floor).<br /><br />Interval – Maximum tries<br />1 – 100<br />2 – 51<br />3 – 35<br />4 – 29<br />5 – 25<br />6 – 21<br />7 – 20<br />8 – 19<br />9 – 19<br />10 – 19<br />11 – 19<br />12 – 19<br />13 – 19<br />14 – 20<br />15 – 20<br />16 – 21<br /><br />So picking any one of the intervals with 19 maximum tries would be fine.Administratornoreply@blogger.com1tag:blogger.com,1999:blog-685070957732806695.post-42365867792738707322010-05-02T13:19:00.002-07:002010-05-02T13:19:48.383-07:008 Identical Balls Problem<b>Question:</b> You are given 8 identical looking balls. One of them is heavier than the rest of the 7 (all the others weigh exactly the same). You a provided with a simple mechanical balance and you are restricted to only 2 uses. Find the heavier ball.<br /><br /><b>Answer:</b> For convenience sake, let’s name the balls 1-8. First we weigh {1,2,3} on the left and {4,5,6} on the right. There are three scenarios which can arise from this.<br /><br />If the left side is heavier, then we know that one of 1, 2 or 3 is the heavier ball. Weigh {1} on the left and {2} on the right. By doing this, we will know if 1 or 2 is heavier. If they balance out, then 3 is the heavier one.<br /><br />If the right side is heavier, then we know that either 4, 5 or 6 is the heavier ball. Weigh {4} on the left and {5} on the right. By doing this we will know if 4 or 5 is heavier. If they balance out, then 6 is the heavier one.<br /><br />If {1,2,3} and {4,5,6} balance out, then we know either 7 or 8 is the heavier one. Weigh both of them to find out which one is heavier.Administratornoreply@blogger.com0tag:blogger.com,1999:blog-685070957732806695.post-49865943558034501662010-05-02T13:19:00.000-07:002010-05-02T13:19:03.708-07:005 Pirates Fight for 100 Gold Coins<b>Question:</b> Five pirates discover a chest containing 100 gold coins. They decide to sit down and devise a distribution strategy. The pirates are ranked based on their experience (Pirate 1 to Pirate 5, where Pirate 5 is the most experienced). The most experienced pirate gets to propose a plan and then all the pirates vote on it. If at least half of the pirates agree on the plan, the gold is split according to the proposal. If not, the most experienced pirate is thrown off the ship and this process continues with the remaining pirates until a proposal is accepted. The first priority of the pirates is to stay alive and second to maximize the gold they get. Pirate 5 devises a plan which he knows will be accepted for sure and will maximize his gold. What is his plan?<br /><br /><b>Answer:</b> To understand the answer, we need to reduce this problem to only 2 pirates. So what happens if there are only 2 pirates. Pirate 2 can easily propose that he gets all the 100 gold coins. Since he constitutes 50% of the pirates, the proposal has to be accepted leaving Pirate 1 with nothing.<br /><br />Now let’s look at 3 pirates situation, Pirate 3 knows that if his proposal does not get accepted, then pirate 2 will get all the gold and pirate 1 will get nothing. So he decides to bribe pirate 1 with one gold coin. Pirate 1 knows that one gold coin is better than nothing so he has to back pirate 3. Pirate 3 proposes {pirate 1, pirate 2, pirate 3} {1, 0, 99}. Since pirate 1 and 3 will vote for it, it will be accepted.<br /><br />If there are 4 pirates, pirate 4 needs to get one more pirate to vote for his proposal. Pirate 4 realizes that if he dies, pirate 2 will get nothing (according to the proposal with 3 pirates) so he can easily bribe pirate 2 with one gold coin to get his vote. So the distribution will be {0, 1, 0, 99}.<br /><br />Smart right? Now can you figure out the distribution with 5 pirates? Let’s see. Pirate 5 needs 2 votes and he knows that if he dies, pirate 1 and 3 will get nothing. He can easily bribe pirates 1 and 3 with one gold coin each to get their vote. In the end, he proposes {1, 0, 1, 0, 98}. This proposal will get accepted and provide the maximum amount of gold to pirate 5.Administratornoreply@blogger.com145tag:blogger.com,1999:blog-685070957732806695.post-91337370917086640102010-05-02T13:18:00.000-07:002010-05-02T13:18:08.261-07:00Is your husband a cheat?A certain town comprises of 100 married couples. Everyone in the town lives by the following rule: If a husband cheats on his wife, the husband is executed as soon as his wife finds out about him. All the women in the town only gossip about the husbands of other women. No woman ever tells another woman if her husband is cheating on her. So every woman in the town knows about all the cheating husbands in the town except her own. It can also be assumed that a husband remains silent about his infidelity. One day, the mayor of the town announces to the whole town that there is at least 1 cheating husband in the town. What do you think happens?<br /><br /><b>Ans:</b><br />A certain town comprises of 100 married couples. Everyone in the town lives by the following rule: If a husband cheats on his wife, the husband is executed as soon as his wife finds out about him. All the women in the town only gossip about the husbands of other women. No woman ever tells another woman if her husband is cheating on her. So every woman in the town knows about all the cheating husbands in the town except her own. It can also be assumed that a husband remains silent about his infidelity. One day, the mayor of the town announces to the whole town that there is at least 1 cheating husband in the town. What do you think happens?Administratornoreply@blogger.com0tag:blogger.com,1999:blog-685070957732806695.post-2000208396874115142010-05-02T13:13:00.001-07:002010-05-02T13:13:16.521-07:00Google Interview QuestionsThis site contains many Google Interview Puzzles and AnswersAdministratornoreply@blogger.com2tag:blogger.com,1999:blog-685070957732806695.post-36737090236150668442010-05-02T13:12:00.001-07:002010-05-02T13:12:23.542-07:00Google Interview QuestionsThis site contains many Google Interview Questions and AnswersAdministratornoreply@blogger.com0