Categories
FollowersBlog Archive
Jobs
Find more freelance jobs
|
Showing posts with label Logical Interview Puzzles. Show all posts
Showing posts with label Logical Interview Puzzles. Show all posts
Sunday, November 21, 2010100 coins dividing into equal heads groups.
Question:
You have 100 coins, 37 of them are heads, rest are tails. Your task is to divide this 100 coins into two groups in which there will be the same number of heads. You are blindfolded and you can flip any number of coins. Solution: Pick any 37 coins and flip all of them. Explanation: Lets take a simple case where 5 coins with 3 heads and 2 tails. HTHHT Lets take 3 coins randomly. Say first,fouth and fifth lets call it S1. Let remaining coins be S2. So we have 2 groups now S1(H,H,T) and S2(T,H) When we flip the selected set S1 it turns into S1F(T,T,H). We have only one head in both the sets. Sunday, March 28, 2010Finding number of on bits (1 bits) given an unsigned integer?
Sol 1:
Simple Iterative solution.(Very slow) int countbits (unsigned int number) { int count = 0; while (number) { count += number & 0x1u; number >>= 1; } return count; } Sol 2: Faster Solution for 32 bit machines. compute 16bits then add then to return full number of ones in 32 bit number. static char ones_in_16bit_num [0x1u << 16] ; int countbits (unsigned int number) { return ones_in_16bit_num [number & 0xffffu] + ones_in_16bit_num [(number >> 16) & 0xffffu] ; } Saturday, March 27, 2010given 2 non-uniform ropes (each burns down in 1hr, but not uniformly), how to measure 45 mins
Ignite one rope on both the ends. Ignite the second rope at only one end. When the first rope burns out, it is 30 mins. Then ignite the unburned end of the second rope. When the remaining rope burns out, it is another 15 mins. So it is 45 mins.
Tuesday, August 11, 2009Design a stack which have O(1) time complexity for finding the minimum element?
This problem can be solved using simple encoding technique. We should use the minimum element to get the next minimum element.Here is a solution which uses O(1) space.PUSH(elem) Operation: In this process we are keeping minimum element on the top of the stack at any given time. And encoding(i.e elem-min) the actual data with minimum and storing. Whenever there is a change in cur minimum data stored in the stack will be negative. This property will be used while poping the elements to find the next minimum. We can define it as shown below. if stack is empty. Store the element twice Else CurMin = POP(); Take difference between elem and Curmin and push it on to stack. Push the least of elem and CurMin on to stack.
POP() Operation: POP process is bit tricky. If minimum is not changing (i.e elements are positive on stack) then return the element + curMin(sitting on stack). If you see a negative stack element then it means there was a shift of curMin (or minimum element is the one which is being popped). In this case we take the current minimum and return it as popped element. We find the new minimum (as current minimum is popped out) by computing curMin-element and push it on top of stack as minimum element. We can summarize it as shown below.curMin = pop();element = pop(); If element is negative then return curMin and push curMin-element on to stack. Else push curMin on to stack and return element + curMin. FindMin(): This is trivial operation as we are storing minimum element on the top of the stack at any given time. So we pop it and return to the caller. FindMin() steps will be...min=pop();push(min);return min; Saturday, April 4, 2009Does the sun always rise in the eastThis is a bit odd question for testing a s/w engineer. But still people ask these Qs. Here is the most appropriate answer i could get.
At the North Pole, there is no such thing as east. Every direction is south. During the six-month polar "day," the sun rises in the south and sets in the south. Similarly at south pole every direction is north.
Does the sun always rise in the east?
This is a bit odd question for testing a s/w engineer. But still people ask these Qs. Here is the most appropriate answer i could get.
At the North Pole, there is no such thing as east. Every direction is south. During the six-month polar "day," the sun rises in the south and sets in the south. The opposite holds for the South Pole, where every direction is north Monday, February 2, 2009Why are manhole covers round rather than square?
This Question was most interviewer's favourite question for years. I hardly find anyone asking it these days. Answer to this question involves some simple maths with commonsense.
All of us know that circle has same diameter in all directions so no matter how you hold it cover cannot be pushed into a circular manhole. Whereas, if it is square, cover can be pushed into manhole via diameter by holding it on edge. Another aspect is, if cover is circular it is easier to roll it so for shorter distances transportation is easy. Saturday, January 10, 2009Simulate seven sided die using 5 sided die?
This Question is asked in different formats like simulate rand7() using rand5() function.
There is no right answer to this question if interviewer specifies that 7 sided die should have equal probability to al the numbers on it(i.e. 1-7). This is because 5 and 7 are coprime and there is no direct mapping available between coprimes. There are several solutions which creates a biased 7 sided die. Sol1: Throw 5 sided die 7 times. sum will be between 7 and 35, with equal probable numbers. Then take the module 7 of the sum and add 1 to it. This solution will produce biased die because probability of 1 is higher here. Sol2: rand7() = ((ran5() + rand5() + rand5() + rand5() + rand5() - 5) % 7) + 1 Saturday, March 1, 2008There are 25 horses and only 5 tracks. No of matches to find top 3 horses.
Problem:
There are 25 horses and only 5 tracks. So only 5 horses can run the race at a time. How many minimum no of races should be conducted to find the 3 best horses? Sol: The answer is 7. Round 1: Take 5 horses at a time. 5 winners of each race will go to round2. Round 2: Winner of this will be top most horse.(call it W1) Round 3: Final race comprises of following 5 horses. h1: 2nd horse of round2 h2:3rd horse of round2 h3:2nd horse of W1's group during round 1 h4:3rd horse of W1's group during round1 h5:2nd horse of h1's group during round1 Friday, February 29, 2008Hat color problem--most asked question in interviews..
http://en.wikipedia.org/wiki/Prisoners_and_hats_puzzle
Two tribes one always tells true other false....
There were 2 tribes living on an island. The east tribal people always tell a lie. The west tribal people always tell the truth. Alan and Bryan came from the same tribe. However, we do not know which tribe they came from. When they were asked the marital status, Alan said: “Both of us are married.” But Bryan said: “I am not married”. Are they really married?
Ans: Both are from same tribe. It means either both of them will lie or both will tell true. If we look at the statements both of them contradicts about Bryan's marital status. It means one of them is not telling truth, as they are from same tribe it concludes that they are from east tribe. It means Bryan is married and Alan's status is unclear. Friday, January 18, 2008Game of Russian roulette!!You are tied to a chair and can,t get up.Here is the gun , six chambers all empty. Now I put two bullets in the gun and I put these bullets in the adjecent chambers. I close the barrel and spin it. I put the gun to your head and pull the trigger. Clik and the slot was empty. Now before we start the interview I want to pull the trigger one more time , which one do you prefer , that I spon the barrel first or that I just pull the trigger ? X being a bullet, _ being and empty chamber. The gun rotates from 1 to 6, and the bullets are in adjacent chambers. The setup: _ X X _ _ _ The interviewer just got a blank, so the current position must be 1,4,5, or 6. If it’s 1, the next one is a bullet. If it’s 4-6, the next one is empty. So if the interviewer just fires again, you have a 1/4 chance of getting shot. If, on the other hand, the interviewer spins again, you’re back to the initial 2/6 or 1/3 chance of getting shot. I’ll take the 1/4 chance, myself. Wednesday, December 5, 2007Four people crossing the bridge optimally
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 it is only 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 1 minute, another in 2 minutes, the third in 5 minutes, and the slow poke takes 10 minutes to cross. How do the campers make it across in 17 minutes?
Sol: When two people walk and their speeds are different then the time taken(cost) to cover some distance will be the time taken by slowest of the two. first take 1, 2 to side2 cost = 2 get 2 back to side1 cost = 2 then take 5,10 to side2 cost = 10 get 1 back to side 1 cost = 1 get 1 and 2 back to side2 cost =2 total cost = 17 100 couples C cheats problem?
Every man in a village of 100 married couples has cheated on his wife. Every wife in the village instantly knows when a man other than her husband has cheated, but does not know when her own husband has. The village has a law that does not allow for adultery. Any wife who can prove that her husband is unfaithful must kill him that very day. The women of the village would never disobey this law. One day, the queen of the village visits and announces that at least one husband has been unfaithful. What happens?
Sol: At the end of Cth day all of the cheats get killed if there are C cheats Five Pirates gold coins division problem.
You have five pirates, ranked from A to E in descending order. The top pirate has the right to propose how 100 gold coins should be divided among them. But the others get to vote on his plan, and if fewer than half agree with him, he gets killed. How should he allocate the gold in order to maximize his share but live to enjoy it? (Assumption : If the top pirate is voted down and gets killed, then the remaining pirates retain their rankings and continue the game, with Pirate 4 now in charge etc.. 2) top pirate also have the voting right and he will not be killed if there is a tie in number of votes)
Ans: 98 coins with top pirate This is a game theory problem which involves some reasoning. Lets start to the solution as if there are only 2 pirates. In this case, the top-ranked pirate (Pirate B) would get 100 gold coins, and Pirate A wouldn't get any, because Pirate A has no way to vote down the plan. Let's work backwards from there. Clearly, Pirate A doesn't want it to come down to just two people. So consider if there were three pirates. Then, Pirate A would accept even just one coin to avoid letting things get down to two people. Thus, if there were three pirates involved, Pirate C should offer Pirate A a single coin; Pirate A would agree because one coin is better than zero. Pirate B could not overrule this plan. Lets say there are 4 pirates, Pirate B would accept even just one coin to avoid letting things get down to three pirates; if it got down to three, as we have shown above, Pirate B would get nothing. Thus, Pirate D could offer to keep 99 coins and pay Pirate B a single coin; Pirate A and Pirate C would get nothing, but they don't have enough votes to override the plan. Coming to actual five pirates case , Pirate A and Pirate C are anxious to avoid letting things get down to four pirates, because then they'd get nothing. So if Pirate E offered Pirate A and Pirate C a single coin each, then they'd vote in favor of the plan, since one coin would be better than nothing. So Pirate E keeps 98 coins for himself. Generalized answer is If there are odd number of pirates then the number of coins with top pirate = total number of coins - number of odd numbers below the number. else the number of coins with top pirate = total number of coins - number of even numbers below the number. Proff for above formulae can be established by following In case of 6 pirates: Pirate F would have to offer a coin to Pirates B and D to satisfy everyone's self-interests. In case of 7 pirates : Pirate G would have to offer a coin to Pirates A, C, and E. Thursday, November 22, 2007Ratio of boys and girls.
In a country in which people only want boys, every family continues to have children until they have a boy. if they have a girl, they have another child. if they have a boy, they stop. what is the proportion of boys to girls in the country?
Solution: It doesn't matter how many kids a family has. The odds of having a boy or girl are always the same. So number of boys and girls in the village is same.
Subscribe to:
Posts (Atom)
|
Popular Posts
|