Search for notes by fellow students, in your own course and all over the country.

Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.

My Basket

You have nothing in your shopping cart yet.

Title: Puzzles For Programming Interviews
Description: Puzzles For Programming Interviews . It contains puzzles with easy explanations that are asked in interviews of many IT companies such as Google, Amazon, Microsoft, etc.

Document Preview

Extracts from the notes are below, to see the PDF you'll receive please use the links above


1) Camel and Bananas Puzzle
Puzzle:
The owner of a banana plantation has a camel
...
The distance between his banana plantation and the market is about 1000 kilometer
...
The camel can carry at the maximum of 1000 bananas at a
time, and it eats one banana for every kilometer it travels
...
If the Camel starts by picking up the 1000 bananas and try
to reach point B, then he will eat up all the 1000 bananas on the way and there will be no bananas left for him
to return to point A
...

Since there are 3000 bananas and the Camel can only carry 1000 bananas, he will have to make 3 trips to carry
them all to any point in between
...

In the first part, P1, to shift the bananas by 1Km, the Camel will have to
1
...
Leave 998 banana after 1 km and return with 1 banana – will eat up 1 banana in the way back
3
...
Leave 998 banana after 1 km and return with 1 banana – will eat up 1 banana in the way back
5
...

So to shift 3000 bananas by 1km, the Camel will eat up 5 bananas
...

Now in the Part P2, the Camel needs to do the following to shift the Bananas by 1km
...
Move forward with 1000 bananas – Will eat up 1 banana in the way forward
2
...
Pick up the next 1000 bananas and move forward – Will eat up 1 banana in the way forward
Note: After point 3 the Camel does not need to return to the starting point of P2
...

After moving to 333 km the camel would have eaten up 1000 bananas and is now left with the last 1000
bananas
...
33 km, I have ignored the decimal part because it will not make a
difference in this example
...

Now, for the last part, P3, the Camel only has to move forward
...
Now he has to cover only 467 km and he has 1000 bananas
...


****************************************************************

2) Ant and Triangle Problem
Question:
Three ants are sitting at the three corners of an equilateral triangle
...
What is the probability that none of the ants collide?
Answer:
So let’s think this through
...
If the ants do not pick the same direction, there will definitely be a
collision
...
There is a one in two chance that
an ant decides to pick a particular direction
...

P(No collision) = P(All ants go in a clockwise direction) + P( All ants go in an anti-clockwise direction) = 0
...
5 * 0
...
5 * 0
...
5 = 0
...
Unfortunately, they have only one torch and the
bridge is too dangerous to cross without one
...

Not all people take the same time to cross the bridge
...
What is the shortest time needed for all four of them to cross the bridge?

Puzzle Solution:
It is 17 mins
...
Then 7 and 10 go and 2 comes back
...


****************************************************************

4) Burning Rope Timer Puzzle
Puzzle:
A man has two ropes of varying thickness (Those two ropes are not identical, they aren’t the same density nor
the same length nor the same width)
...
He actually wants to measure 45 mins
...

He can’t cut the one rope in half because the ropes are non-homogeneous and he can’t be sure how long it will
burn
...
After half an hour, the first one
burns completely and at this point of time, he will burn the other end of the second rope so now it will take 15
mins more to completely burn
...
e
...


****************************************************************

5) Heaven or Hell Puzzle
Problem: You are standing before two doors
...

There are two guardians, one by each door
...

You can only ask one question to one of them in order to find the way to heaven
...


Solution:
The question you should ask is “If I ask the other guard about which side leads to heaven, what would he
answer?”
...
So you can chose the other path to continue your journey to heaven
...

Here is the explanation if it is yet not clear
...


If you ask the guard which speaks truth about which path leads to heaven, as he speaks always the truth, he
would say “left”
...

Similarly, if you ask the liar about which path leads to heaven, he would say “right”
...
So
in any case, you would end up having the path to hell as an answer
...


****************************************************************

6) 10 Coins Puzzle
Problem: You are blindfolded and 10 coins are place in front of you on table
...
You are told that there are 5 coins head up, and 5 coins tails
up but not which ones are which
...

This puzzle was asked in Yahoo Interview
...
Now, flip all the coins in one of the pile
...

So initially there are 5 heads, so suppose you divide it in 2 piles
...
A neighboring queen plots to
kill the bad king and sends a servant to poison the wine
...
Alas, the guards don’t know which bottle but
know that the poison is so strong that even if diluted 100,000 times it would still kill the king
...
The bad king decides he will get some of the prisoners in his vast dungeons
to drink the wine
...
Explain what is in mind of the king, how will he be able to do so ? (of
course he has less then 1000 prisoners in his prisons)
Solution:
Think in terms of binary numbers
...

Number the bottles 1 to 1000 and write the number in binary format
...
Let prisoner 10 take a sip from every bottle with a 1 in its most significant bit
...

prisoner = 10 9 8 7 6 5 4 3 2 1
bottle 924 = 1 1 1 0 0 1 1 1 0 0
For instance, bottle no
...
That way if bottle no
...

After four weeks, line the prisoners up in their bit order and read each living prisoner as a 0 bit and each dead
prisoner as a 1 bit
...

1000 is less than 1024 (2^10)
...


****************************************************************

8) 3 Mislabeled Jars
Problem:

This problem is also called Jelly Beans problem
...

You have 3 jars that are all mislabeled
...

You are allowed to pick as many fruits as you want from each jar to fix the labels on the jars
...
Suppose you pick from jar labelled as Apple and Oranges and you got Apple from it
...
So it has to be Apple jar
...

Similar scenario applies if it’s a Oranges taken out from the jar labelled as Apple and Oranges
...


****************************************************************

9) Red and Blue Marbles
Problem:
You have two jars, 50 red marbles and 50 blue marbles
...
When
picking, you’ll first randomly pick a jar, and then randomly pick a marble out of that jar
...

Answer:
Say we put all the red marbles into JAR A and all the blue ones into JAR B
...
You would still end up with a chance of 50%
...
Now that you have 49 red marbles left in the other jar, you have a nearly even chance of
picking a red marble (49 out of 99)
...
The gold bar is segmented into
seven connected pieces
...
What and where are the
fewest number of cuts to the bar of gold that will allow you to pay him 1/7th each day?
Solution:
Lets split the chain as,
1,2,4 (3 parts)
Day 1: Give A (+1)
Day 2: Get back A, give B (-1, +2)
Day 3: Give A (+1)
Day 4:Get back A and B, give C (-2,-1,+4)
Day 5:Give A (+1)
Day 6:Get back A, give B (-1,+2)
Day 7:Give A (+1)

Trick: Whenever it is said that you have to measure 15kg and minimum number of weights
required for this?
Then always start with 2^0 and go to less than 15 i
...
{1,2,4,8}
...


****************************************************************

11)

100 Doors Puzzle

Problem :
You have 100 doors in a row that are all initially closed
...
the first time through you visit every door and toggle the door (if the door is closed, you
open it, if its open, you close it)
...
the third
time, every 3rd door (door #3, #6, #9), ec, until you only visit the 100th door
...
so has 1 &
38, 2 & 19
...
For every pair
of divisors the door will just end up back in its initial state
...
9 has the divisors 1 & 9, 3 & 3
...
only perfect square
doors will be open at the end
...
You put your hand in the bag and take off two at a time
...
If they’re of different colors, you add a Red ball to
the bag
...
When you take the two balls out,
you don’t put them back in, so the number of balls in the bag keeps decreasing
...

a) If we take off 1 Red and 1 Blue, in fact we will take off 1 Blue
b) If we take off 2 Red, in fact we will take off 2 Red (and add 1 Blue)
c) If we take off 2 Blue, in fact we will take off 1 Blue
So In case of (a) or (c), we are only take off one Blue ball
...

1) 20 Blue, 10 Red balls
If there are 10 (even) number of Red balls, we cannot have one single Red ball left in the bag, so the last ball
will be Blue
...
of Red balls is odd, there will be one single Red ball in the bag with other Blue balls, and
whenever we remove 1 Red and 1 Blue ball, we end up taking off only the Blue ball
...


****************************************************************

13)

Squares on a chess board?

Problem:
How many squares are on a chess board?

If you thought the answer is 64, think again!
How about all the squares that are formed by combining smaller squares on the chess board (2×2, 3×3, 4×4
squares and so on)?
A 1×1 square can be placed on the chess board in 8 horizontal and 8 vertical positions, thus making a total of 8
x 8 = 64 squares
...
There are 7 horizontal positions and 7 vertical positions in
which a 2×2 square can be placed
...
So we have 7 x 7 = 49 2×2 squares
...
So here’s a break down
...
n ^2
Total = 64 + 49 + 36 + 25 + 16 + 9 + 4 + 1 = 204 squares

****************************************************************

14)

Probability of having boy

In a country where everyone wants a boy, each family continues having babies till they have a boy
...
This question might be a little old to
be ever asked again but it is a good warm up
...
The number of girls can be
calculated by the following method
...


****************************************************************

15)

13 Caves And A Thief Puzzle

Problem:
There are 13 caves arranged in a circle
...
Each day the thief can move to any
one of adjacent cave or can stay in same cave in which he was staying the previous day
...

What is the minimum number of days to guarantee in which cops can catch the thief?

Note:
Thief may or may not move to adjacent cave
...

Solution (not confirmed):
Lets assume the thief is in cave C1 and going clockwise and cops start searching from cave C13 and C12 on
your first day
...

So basically the aim is to check C13 everyday so that if thief tries to go anti clockwise you immediately catch
it and if goes clockwise cops will catch him in maximum 12 days (this include the case where he remains in
Cave C1)
...

Another solution: one move clockwise another anti-clock, hence minimum days 7

****************************************************************

16)

Prisoners and Hats Puzzle

Problem:
Four prisoners are arrested for a crime, but the jail is full and the jailer has nowhere to put them
...

The jailer puts three of the men sitting in a line
...

He gives all four men party hats
...
The fourth man behind the screen can’t see or be seen by any other prisoner
...

If any prisoner can figure out and say to the jailer what color hat he has on his head all four prisoners go free
...
The puzzle is to find how the
prisoners can escape, regardless of how the jailer distributes the hats
...
C and D realise this
...
If B and C had the same colour hat then this would let D know that he
must have the other colour
...
C
realises that his hat must be different to B’s otherwise D would have answered
...


****************************************************************

17)

Age of 3 children – Mathematical Puzzle

Problem:
Two old friends, Jack and Bill, meet after a long time
...

Jack: That’s awesome
...

Jack: Cool… But I still don’t know
...

Jack: Oh now I get it
...
To do it, first write down all the real possibilities that the number on that
building might have been
...
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
...
For any other number, the answer is unique and the Jack
would have known after the second clue
...
The clue that the eldest kid just started
taking piano lessons is really just saying that there is an “oldest”, meaning that the younger two are not twins
...

The answer is 3, 3 and 8
...
Their professor wanted them to test
...
He also added that he
has at least one hat with red color and the no
...
of red hats
...

Professor goes out and comes back after 20 minutes but nobody was able to answer the question
...
So he decides to give them final 5 minutes
...

So what is the answer? and why?
Answer:
After first interval of 20 minutes :
Lets assume that their is 1 hat of red color and 8 hats of black color
...


Now we know that after first interval nobody was able to answer the prof that means our assumption is wrong
...

After second interval of 10 minutes :
Assume that their are 2 hats of red color and 7 hats of black color
...

Now we know that after second interval nobody was able to answer the prof that means our assumption is
again wrong
...

After third interval of final 5 minutes :
Now assume that their is 3 hats of red color and 6 hats of black color
...

Now we know that this time everybody was able to answer the prof that means our assumption is right
...
Now as everybody gave the answer so there can be a doubt that only those 3
students know about it how everybody came to know ?
Then here is what i think, the professor gave them FINAL 5 minutes to answer, so other guys will think that
the professor expects the answer after 3rd interval (according to prof it must be solved after 3 intervals), so this
is the clue for others
...
It’s her anniversary,
and you want to give her the cakes you’ve made
...
Before you can cross their bridge, you have to give them half of the cakes you
are carrying, but as they are kind trolls, they each give you back a single cake
...
Which leaves you with 2
cakes after every bridge
...


****************************************************************

20)

Handshake Problem

Problem:
At a party, everyone shook hands with everybody else
...
How many people were at
the party?

This question is asked in Infosys written
...


****************************************************************

21)

Riding Against the Wind Puzzle

Problem: A horse rider went a mile in 5 minutes with the wind and returned in 7 minutes against the wind
...
We find this answer to be incorrect, because the wind has helped him for only 2 minutes, while it has
worked adversely for 3 minutes
...
5 mile 3 minutes, and 1 mile
in 3 minutes against the wind
...
5 miles in 6 minutes gives his actual speed, because the wind helped him just as much as it has
retarded him, so his actual speed for a single mile without any wind would be (2
...
One coin is taken from the bag and put away
...

Probability of getting a one rupee coin in the first and second draw = x/(x + y) × (x – 1)/(x – 1 + y)
Case II: Let the first coin removed be fifty paise coin One rupee coins left = x Fifty paise coins left = y – 1
...


****************************************************************

23)

River Crossing Puzzle

Problem:
Sailor Cat needs to bring a wolf, a goat, and a cabbage across the river
...
If he leaves the wolf and the goat alone together, the wolf will eat the goat
...

How can he bring all three safely across the river?
Solution:
The trick to this puzzle is that you can keep wolf and cabbage together
...
He will go to the other side of the river with the goat
...
When he reaches the other side he
will keep the cabbage there and will take goat back with him
...
He will
return back and will take goat along with him
...


****************************************************************

24)

3 Doors and Heaven

Problem:
A person dies, and he arrives at the gate to heaven
...
One of the door leads to
heaven, second one leads to a 1-day stay at hell and then back to the gate and the third one leads to a 2 day stay
at hell and then back to the gate
...
How
long will it take the person to reach heaven?

Solution:
According to probability 1/3 of the time, the door to heaven will be chosen, so 1/3 of the time it will take 0
days
...
Similarly,
1/3 of the time, the 2 day door is chosen, of those, the right door will be chosen after the 2 days
...
1/3 of the cases are done in 0 days as before
...
1/3 are 2
+ N
...

So it will take on average 3 days to reach to heaven
...

Salary of A: i
Salary of B: j
Salary of C: k
Salary of D: l
A passes to B (i + a) where a is a number that A knows B takes this a passes to C (i + j + a + b)
...
D takes this and passes to A (i + j + k + l + a + b + c + d)
Now one after another they remove their constants
...

Thus Finally D gets x + y + z + u + d
...

So the average is:(i + j + k + l) / 4
...

Other solution:
Instead of each one passing a constant, only A can pass his salary + constant
...


****************************************************************

26)

Probability of picking 2 socks of same color

Problem :
There are 6 pairs of black socks and 6 pairs of white socks
...

This question was asked in Aamaon
...
Some of the glasses are upright (up) and some upsidedown (down)
...
The glasses may be re-arranged in turns subject to the following rules
...
After each turn the table is rotated through a random angle
...
The algorithm must be non-stochastic i
...
it must not depend on luck
...

 On the second turn choose two adjacent glasses
...
If
the other is down, turn it up as well
...

 On the third turn choose a diagonally opposite pair of glasses
...
If both are up, turn one down
...

 On the fourth turn choose two adjacent glasses and reverse both
...
Otherwise there are now two glasses down and they must be diagonally opposite
...
The bell will ring for sure
...
The fox cannot swim, and the duck
cannot take flight from the water
...
Assuming the fox and duck pursue
optimum strategies, is it possible for the duck to reach the edge of the pond and fly away without being eaten?
If so, how?

Solution:
From the speed of the fox it is obvious that duck cannot simply swim to the opposite side of the fox to escape
...
Since fox have to travel half of the circumference Pi*r
and Pi*r < 4r
So how could the duck make life most difficult for the fox? If the duck just tries to swim along a radius, the
fox could just sit along that radius and the duck would continue to be trapped
...

Let the duck rotate around the pond in a circle of radius r/4
...
Now reduce the radius the duck is circling by a very small amount (Delta)
...

Say, the duck circles the pond at a distance r/4 – e, where e is an infinitesimal amount
...
Once the duck is able to
gain 180 degrees over the fox, the duck would have to cover a distance of 3r/4 + e to reach the edge of the
pond
...
e the 180 degrees)
...
14*r at four times the speed
...
14*r distance is left)
The duck would be able to make it to land and fly away
...
One of them is defective and weighs less than others
...
In 2 weighing, how do you find the defective one?

Solution:
Defective ball is light
Make three Groups G1 – 3 balls G2 – 3 balls G3 – 2 balls
First weight- G1 and G2 if G1 = G2 then defective ball in G3 ,
weigh the the 2 balls in G3 if EQUAL then 3rd ball of G3 is defective
else whichever lighter in 1st or 2nd is defective ball
else if G1 < G2 defective ball in G1
weigh 1 and 2 ball of G1 if EQUAL then 3rd ball of G1 is defective
else whichever lighter in 1st or 2nd is defective ball
else if G1 > G2 defective in G2
Again in 1 comparison we can find the odd ball
...


****************************************************************

30)

Heavy and Light Balls Puzzle

Puzzle: You have 2 ball of each A,B,C colors and each color have 1 light and 1 heavy ball
...
Find out weight type of each ball in minimum chances
...

This puzzle was asked in many interviews – Drishti-soft, Yahoo, Infoedge
...
Now, comparing those balls with other balls but this
will take 3 chances for each color type ball
...


So, Make a table for all conditions for all 6 balls that will help in understanding and solving this problem
...

Case 1:
Equal, if equal weight simply B1,B2 will solve the problem
...
Also just that A1>=C1
...

Case 3:
If A1+B1 > B2+C1
Similar to above case we can check it
...
Criminal has to take
decision to open one of the doors
...
They may be both tigers, both ladies or one of each
...
Of course, the criminal would prefer to be married than eaten alive
...

The statement on door one says, “In this room there is a lady, and in the other room there is a tiger
...

The criminal is informed that one of the statements is true and one is false
...

How?
Lets assume statement on the first door is true then second statement will also be true(as there will be a lady in
one door and a tiger in other door), but as we already know that only one statement can be true, so first
statement can not be true
...


****************************************************************

32)

Sand Timer Puzzle

This Puzzle is one of the most commonly asked interview puzzle
...
One can measure 7 minutes and the other sand timer can measure 11
minutes
...

You have to measure 15 minutes using both the timers
...

Time Remaining in 11 minutes timer – 4 minutes
Reversing the 7 minutes timer – 4 minutes will elapse
...


Once 11 minutes gets over reverse the 11 minutes timer again to use that 3 minutes
...

Now Reverse 7 minutes timer to measure 7+8 = 15 minutes
...
You can choose only one of the doors among the three
...

After you choose one of the doors angel reveals one of the other two doors behind which there is a nothing
...

You don’t know behind which door we have nothing
...
So probability of getting the jackpot – 1/3
...
So the angel will either open door no 2 or
door no 3
...

Case ->
Case 1 :

Door1
Jackpot

Door2
Nothing

Door3
Nothing

Case 2 :
Case 3 :

Nothing
Nothing

Jackpot
Nothing

Nothing
Jackpot

Want to keep your guess:
Let’s suppose that you guessed correctly
...
So in that case, by keeping your choice, the probability that you win is 1/3 x 1
= 1/3
...
In that case, the remaining door is guaranteed to be the correct door
...

Your total chances of winning by keeping your guess is: 1/3 + 0 = 1/3
...
By changing your guess the probability that you win is 1/3 x 0
= 0
...
Again, this means that the remaining door must be the correct one
...

Your total chances of winning by changing your guess is: 2/3 + 0 = 2/3
...


****************************************************************

34)

10 identical bottles of pills

This is a very popular brain teaser with many companies
...
Out of 10 bottles 9 have 1
gram of pills but 1 bottle has pills of weight of 1
...
Given a measurement scale, how would you find the
heavy bottle? You can use the scale only once
...
Ideally you would have (10)*(11)/2=55 pills weighing 55 grams, when you
put the entire pile of pills on the weighing scale
...

If it is
...
2 more, gram 2nd bottle has heavy pills, if it is

...


****************************************************************

35)

How Much Money He had Initially ?

Puzzle:
One person has some money in his pocket, He visits four temple on the way
...
100 in each temple thus his pocket gets empty after he returns from the
fourth temple
...
e
...
e
...
75
Answer: Rs 93
...

Puzzle: Albert and Bernard just became friends with Cheryl, and they want to know when her birthday is
...

May 15 May 16 May 19
June 17 June 18
July 14 July 16
August 14 August 15 August 17
Cheryl then tells Albert and Bernard separately the month and the day of her birthday respectively
...

Bernard: At first I don’t know when Cheryl’s birthday is, but I know now
...

So when is Cheryl’s birthday?
Solution:
The solution involves using logic to deduce the dates which can’t possibly be Cheryl’s birthday
...

Albert, having seemingly been told the month rather than the day, first says he doesn’t know when her birthday
is – eliminating both 18 and 19 as possible days
...
But, as the question says, Albert knows Bernard does not,
meaning that Cheryl has said her birthday is in either July or August
...

If Cheryl told Bernard her birthday was on the 14th, then he would not have known but, he does, meaning it
can’t be on the 14th
...

After Bernard speaks, saying he knows the birthday given that information, it eliminates August from being a
contender since he still wouldn’t have known
whether it was August 15 or 17
...


****************************************************************

37)

Prove that p^2 – 1 is Divisible by 24

Problem:
Prove that p^2 – 1 is divisible by 24 if p is a prime number greater than 3?
Solution:
The most elementary proof , without explicitly mentioning any number theory: out of the three consecutive
numbers p–1, p, p+1, one of them must be divisible by 3; also, since the neighbors of p are consecutive even
numbers, one of them must be divisible by 2 and the other by 4, so their product is divisible by 3⋅2⋅4=24 —
and of course, we can throw p out since it’s prime, and those factors cannot come from it
...
The servants with seven legs always lie, but
the servants with either six or eight legs always say the truth
...

What is the colour of the servant that says the truth?
This is very famous logical puzzles
...

Lets assume that one of them is telling the truth and then try to prove that
...

Lets say blue is telling the truth: so the blue one has either 6 or 8 legs
...
So our total legs becomes: 6 + 7 + 7 + 7 = 27 legs or 8 + 7 + 7 + 7 = 29 legs
...

If you follow this same logic for all of them, you realize that only the Green octopus can be telling the truth as
the number of legs adds up
...

1st one says : at least one is wrong
...

3rd one says : at least three are wrong
...

and so on
...

How many statements are actually wrong and how many actually right ?
Solution:
100th statement is definitely wrong because it says at least 100 are wrong
...

=> 100th statement is wrong and
=> 1st statement is correct
...
)
But 99th statement says that atleast 99 are wrong
...

calculating so on…
50 statements are right (the first 50 ones)
remaining 50 statements are wrong
...
As
soon as they enter the tunnel a supersonic bee flying at 1000 mph starts from one train and heads toward the
other one
...
How far did the
bee travel?
Solution:
This puzzle is a little tricky one
...

The tunnel is 200 miles long
...
The bee is traveling 1000 mph for an hour (since its flying the whole time the trains are
racing toward one another) – so basically the bee goes 1000 miles
...
The distribution between each of the numbers must be uniform
...
Each rand3() will be a decision
...

We try to get maximum bunches of 8 as we can (1 – 16, 2 sets of 8)
...
Since the probability of getting each of 16 values are the same every time, trying again won’t affect
their probabilities
...

6*rand3() + rand3() – 3
See, how this can be used?
1
...
So, there
are total 18 combinations possible
...
The range of values returned by the above equation is 1 to 18, each integer occurring exactly once
...
If the value of the equation comes out to be less than 17, return modulo division by 8 followed by adding 1
...
The probability of returning each integer thus becomes 1/8
...
Pots are arranged in an arbitrary sequence in a row
...
How many days she can do this or how many such
arrangements are possible ?
Solution:
we have (62)=15 different pairs of pots
...
Since all these
pairs should be different, the number of days is at most 15/5=3
...

123456
246135
362514

****************************************************************

43)

3 Baskets And 4 Balls Puzzle

Puzzle: You have 3 baskets and each basket contains exactly 4 balls, each balls is of the same size
...

If you were blindfolded, and lightly shook each basket so that the balls would be randomly distributed, and
then took 1 ball from each basket, what chance is there that you would have exactly 2 red balls?
Solution:
There are 64 different possible outcomes, and in 9 of these, exactly 2 of the balls will be red
...

A other way to solve the problem is to look at it this way
...

There is a 4
...

Take the first one, for example: 25% chance the first ball is red, multiplied by a 25% chance the second ball is
red, multiplied by a 75% chance the third ball is not red
...
6875% chance of any one occurring by 3, & you get 14
...
Each of the dogs starts chasing the dog in the
clockwise direction
...
How long does it take for the dogs to catch each other
and where?
Solution:
Each dog started at the corner and moving symmetrically
...
Lets assume v
...

If we see realtive speed of the dog1 (v1), w
...
t dog 2, it changes perpendicularly
...

So if they have started at corners with the distance of the length of the square (d)
...
They’ll meet at the center
...

What will the cashier do?

Solution:
The smallest amount of one-dollar and two-dollar bills the cashier may give to the old man is 1×1 + 10×2 = 21
...
e
...
Out of all these numbers only 105 can be added to a multiple of 5 to sum up to make 200
altogether
...

Therefore, the cashier must give 5 one-dollar bills, 50 two-dollar bills and 19 five-dollar bills
...
The number of candidates for the first, second and third posts
are 3,4 and 2 respectively
...
The probability she doesn’t
get an offer from the second is 3/4, and the probability she doesn’t get an offer from the third is 1/2
...
Here we are assuming (unrealistically)
independence
...

We made the totally unreasonable assumption that job offers are given at random, that if there are 3 people
interviewed, exactly one, chosen at random, will get an offer
...
You have no idea about which horse is better than other
...


Solution:
Draw out a table
In problems like this, it helps tremendously to create some sort of visual aid that you can refer to
...


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

Let’s say that we have 5 races of 5 horses each, so each row in the table above represents a race
...
In each row, the fastest
horses are listed in descending order, from the fastest (extreme left) to the slowest (extreme right)
...
In
the second race X6 was the fastest, X7 was the second fastest and so on
...
But, does that mean we have the 5 fastest horses? Think
about that for a second
...
Because, what if
the 5 fastest horses just happened to be in the first race – so X1 X2 X3 X4 X5 are the fastest horses
...
Remember we haven’t compared all the horses to each other since
we can only run 5 horses in a race, so that is still a possibility
...


Work through a process of elimination
Well, now that we’ve had 5 different races, we can eliminate the slowest 2 horses in each group since those
horses are definitely not in the top 3
...
So what can we do with that information?
Well, we can race those 5 horses against each other (X1, X6, X11, X16, and X21) and that would be the 6th
race
...

What other horses can we eliminate after this 6th race? Well, we can automatically eliminate all the horses that
X16 and X21 competed against in the preliminary races – since X16 and X21 are not in the top 3 then we also
know that any horse that’s slower than those 2 is definitely not in the top 3 either
...

Now, we also know that X1 is the fastest horse in the group since he was the fastest horse out of the 5 group
leaders
...
Are there any other horses that we can eliminate from further
races? Well, actually there are
...
X7
could only possibly be the 3rd fastest, and since X8 is slower than X7, we can safely eliminate X8
...

So, all together we can eliminate these horses after the 6th race: X17 X18 X22 X23 X16 X21, X12, X13, X8
and X1
...


****************************************************************

48)

2 Eggs 100 Floors Puzzle

There is a building of 100 floors
-If an egg drops from the Nth floor or above it will break
...

You’re given 2 eggs
...

This follows the below logic
...

Say for example, I throw the egg from 10th floor, and it breaks, I wíll go to floor 1 to 9 to find out the floor
...
I
...

10,20,30,40,50,60,70,80,90,100,91,92,93,94,95,96,97,98,99
To find optimum solution, let’s try this:
If for every n, egg doesnt break, instead of going to next n, go to N-1, this would save us one drop as we are
doing a linear search with second egg when egg1 breaks…
So the series would look something like this
...

We can write it as
...
e
...

On their ship, they decide to split the coins using this scheme:
The oldest pirate proposes how to share the coins, and ALL pirates (including the oldest) vote for or against it
...
Otherwise, the pirate proposing
the scheme will be thrown overboard, and the process is repeated with the pirates that remain
...


Assuming that all 5 pirates are intelligent, rational, greedy, and do not wish to die, (and are rather good at math
for pirates) what will happen?

Answer :
To understand the answer,
we need to reduce this problem to only 2 pirates
...
Pirate 2 can
easily propose that he gets all the 100 gold coins
...

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
...
Pirate 1
knows that one gold coin is better than nothing so he has to back pirate 3
...
Since pirate 1 and 3 will vote for it, it will be accepted
...
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
...

Smart right? Now can you figure out the distribution with 5 pirates? Let’s see
...
He can easily bribe pirates 1 and 3 with one gold coin
each to get their vote
...
This proposal will get accepted and provide the
maximum amount of gold to pirate 5
...
According to farmer the oldest son should get
half of the horses,the middle son should get one third of the horses and the youngest son should get one ninth
of the horses
...
As the
sons were fighting on how to divide the horses a traveling mathematician came and heard their problem
...

What was the advice given and how the group of horses were divided?

Solution:
Well, this puzzle is interesting
...


Let’s see the problem first
...

1st son — half of the horses (17/2)=8
...
66
3rd son — one ninth of horses (17/9)=1
...
What will the traveling
mathematician do to solve it
...
He will add his horse to the group of horses
...
Now let’s see the
scenario again
...


****************************************************************

51)

Free the prisoners puzzle

The warden meets with 23 new prisoners when they arrive
...
But after today, you will be in isolated cells and will have no communication with one another
...
I am not telling you their present positions
...

“After today, from time to time whenever I feel so inclined, I will select one prisoner at random and escort him
to the switch room
...
He must flip one
switch when he visits the switch room, and may only flip one of the switches
...

“No one else will be allowed to alter the switches until I lead the next prisoner into the switch room
...
I may choose the same guy three times in a row, or I may jump around and
come back
...

“Given enough time, everyone will eventually visit the switch room the same number of times as everyone
else
...

“If it is true, then you will all be set free
...
You will be carefully monitored, and any attempt to break any of these rules will result in
instant death to all of you”
What is the strategy they come up with so that they can be free?
Solution:

The switch #1 is used to COUNT
...

The prisoners elect a leader and stablish the following rules and behaviours:
– Only the leader can turn the switch #1 off
...

– Leader’s behavior:
If the switch #1 is on, turn off the switch #1 and count 1 visit more ( # of visits starts from zero)
...

– Other’s behavior:
If the switch #1 is turned off and I never turned the switch #1 on, then turn the switch #1 on
...


****************************************************************

52)

Find the survivor

100 people standing in a circle in an order 1 to 100
...
1 has a sword
...
e
...
2) and
gives sword to next to next (i
...
3)
...
Which number survives
at the last?
Solution:
Consider the case when there are 2^n numbers in the circle
...

In the given question, 1 kills 2, 3 kills 4 and so on till 71 kills 72
...
64
people remain in the circle
...
So the first guy after 72 will be the new number 1 in a circle of
2^6
...


****************************************************************

53)

The Man in the Elevator

A man lives on the tenth floor of a building
...
When he returns he takes the elevator to the seventh floor and walks up the stairs
to reach his apartment on the tenth floor
...
He hates walking so why does he do it?
This is probably the best known and most celebrated of all lateral thinking puzzles
...

Although there are many possible solutions which fit the initial conditions, only the canonical answer is truly
satisfying
...
We can conclude this because on rainy
day he had Umbrella and when other people are in elevator then they will help him
...


****************************************************************

54)

The Puzzle of 100 Hats

100 persons are standing in line, each facing the same way
...
In fact, a person knows the hat color only of those
persons standing ahead of him in line
...
These exclamations can be heard by all
...

What strategy should the 100 persons use in order to get as high a score as possible, regardless of how the hat
colors are assigned? (That is, what strategy achieves the best worst-case score?)
For example, if everyone exclaims “red”, the worst-case score is 0
...
If every other person exclaims the hat color of the person immediate in front
and that person then repeats the color he has just heard, then the worst-case score is 50
...

Solution:
The person standing at the back of the line can see all the people
...
This means the person in the back
can see 99 people in blue and red hats in front of him
...
The number of red and blue hats the last person can see is not equal
...
Either the red or the blue hats contain an odd number of hats, but not both
...
The person in
the back of the line will begin by counting all the hats, and saying whatever color has an odd number of
hats
...

For example, if the person in the back sees an odd number of red hats, he will declare “red”
...

This process will work if every person in line keeps track of which colors have an odd or even count
...


****************************************************************

55)

Random Airplane Seats

People are waiting in line to board a 100-seat airplane
...
He gets on the plane
but suddenly can’t remember what his seat number is, so he picks a seat at random
...

The flight is full and you are last in line
...

The probabilty is indeed 1/2
...
The probabilty that Steve chooses his assigned seat is equal to the probability that he chooses your assigned
seat
...
In case that Steve would choose neither his own seat nor yours, then there are two alternatives: if somebody
else would choose Steve’s seat at random, then you would get your assigned seat; otherwise you would be left
with the Steve’s seat
...
With every person choosing a seat at random
(including Steve), there are there possible outcomes:
1
...
chooses the Steve’s seat, or
3
...

Notice, that the probabilty of choosing Steve’s seat is always equal to probabilty of taking your seat
...
not, is even
...

Since the probability of someone taking your place is always equal to the probabilty of someone taking Steve’s
place (and this also applies to the penultimate passenger with only two seats left), the probabilty of you getting
your assigned seat is in the end 50%
...


Solution:

OR,

****************************************************************

57)

Measure 9 minutes from 2 hourglasses puzzle

Using only a Four-minute hourglass and a seven-minute hourglass,
How will you measure exactly nine minutes?
Restriction:- Without the process taking longer than nine minutes
...
We reverse it at 7th minute
And, 4+4=8 Minutes from Second Hourglass
8(from 1)-7(From 2) = 1 Minute
TOTAL = 9 MINUTES
EXPLANATION-

– 8 Minutes
– 1 Minutes

0 Minutes – Start both hourglass at the same time
...
Flip the four minute glass
...
Flip the seven minute glass
...
Flip the
seven minute glass
...


****************************************************************

58)

Defective stack of coins puzzle

There are 10 stacks of 10 coins each
...
However, one stack of coins is defective and each coin in that stack weights only 9
gms
...

In total we will have 55 coins
...

If stack 1 is defective, the measure would read 549 gms
...

and so on
...


****************************************************************

59)

Box with Defective Balls

Here is a situation
...

Now among the boxes, one of the box comprises of defective balls with each defective ball weighing 9 grams
...

Can you find out which box contains defective balls?
Solution:
The question can practically disarm you
...

Let us name the boxes with a number - Box1, Box 2
...
Now you must be familiar with the weights of
the balls precisely
...

What you have to do is pick one ball from Box 1, 2 balls from Box 2, 3 balls from Box 3,
...
Thus after taking balls from every box in such a manner, you will finally have 55 balls in total
...

If all the balls were weighing adequate, the combined weight should be 55 x 10 grams = 550 grams
...
Here is the tricky part and explains why we
took different number of balls from each box
...
If
the Box 2 is defective, the total weight will be less than 2 grams
...


****************************************************************

60)

Tower of Hanoi Solution

The binary numbers, considered in numerical order, comprise a set of sequential moves that
will allow you to solve the game of the Tower of Hanoi with the minimum number of moves,

specifically
2n - 1
where n is the number of disks
...
The position of that "1" from the right indicates the disk to move
...
The second "1" from the
right indicates the location of the reference disk
...
If there is an odd number of zeros between these two "1"s, the
moving disk is moved so as NOT to cover the reference disk
...


13

01011

Move disk 1 to cover disk 2
...


18

10010

Move disk 2 to cover disk 5
...


17

10001

Move disk 1 NOT to cover disk 5
...
Try it with the interactive tower in Towers of Hanoi Puzzle
...


Move

Binary Number

Interpretation

1

00001

Move disk 1 to empty peg
...


3

00011

Move disk 1 to cover disk 2
...


5

00101

Move disk 1 NOT to cover disk 3
...


7

00111

Move disk 1 to cover disk 2
...


9

01001

Move disk 1 to cover disk 4
...


11

01011

Move disk 1 to cover disk 2
...


13

01101

Move disk 1 NOT to cover disk 3
...


15

01111

Move disk 1 to cover disk 2
...


17

10001

Move disk 1 NOT to cover disk 5
...


19

10011

Move disk 1 to cover disk 2
...


21

10101

Move disk 1 NOT to cover disk 3
...


23

10111

Move disk 1 to cover disk 2
...


25

11001

Move disk 1 to cover disk 4
...


27

11011

Move disk 1 to cover disk 2
...


29

11101

Move disk 1 NOT to cover disk 3
...


31

11111

Move disk 1 to cover disk 2
...

What is the maximum number of runs that a batsman can score in an ideal case ?
Note:”Here we assume ideal and little practical scenario
...


Answer:
49*(6*5+3)+(6*6)= 1653
From Over 1 to 49:
1st ball:- 6 runs(hit six)
2nd ball:- 6 runs(hit six)
3rd ball:- 6 runs(hit six)
4th ball:- 6 runs(hit six)
5th ball:- 6 runs(hit six)
6th ball:- 3 runs(took 3 runs between the wickets and take back the strike)
= >49*(6*5+3)
50th Over:
Hit six sixes in a row
...


****************************************************************

63)

Prisoner and Policeman Puzzle

Policeman decided to punish the Prisoner and asked him to make a statement
...

If the statement is held true by Policeman, the Prisoner will be hanged to death and if the statement is held false, the
Prisoner will be shot dead
...

If Policeman says the statement is false, the Prisoner will be shot dead which will make the statement true
...
It can’t be any year in 1900 because that would result in a day
of 91, same for 1800 down to 1400
...

So
what’s
the
latest
year
in
1300
that
would
make
a
month?
When you first look at it, 12th month comes to mind as we have to find the latest date, so it seems it would be 1321
...
Which means the correct date would be 08/31/1380
...
Each tyre can travel a maximum distance of 20000 miles before wearing off
...

Answer: 25000

Divide the lifetime of spare tire into 4 equal part i
...
, 5000 and swap it at each completion of 5000 miles distance
...

5000 KMs: Replace A with S
...


****************************************************************
66) Newspaper Puzzle
Question: A newspaper made of 16 large sheets of paper folded in half
...

The first sheet contains pages 1, 2, 63, 64
...
What are the other pages that this sheet contains?
Answer:
On the back of 45, it is 46
...


OR, we can also make pairs like
1+64=65
2+63=65
Therefore, 65-45=20
65-46=19
So the four pages in this sheet are 19, 20, 45, 46
...
You are given 31 dominos,
and a single domino can cover exactly two squares
...
Therefore, 31 dominos will
take 31 white square and 31 black squares exactly
...
Hence it is not possible to do so
...

Answer:
Dice 1: 0 1 2 3 5 7
Dice 2: 0 1 2 4 6 8
Explanation:
You have to show 11, 22 so 1 and 2 should be present in both dices, similarly to show 01, 09
...


****************************************************************

69) Strategy for a 2 Player Coin Game
Consider a two player coin game where each player gets turn one by one
...
The player that collects coins with
more value wins the game
...

Note that the strategy to pick maximum of two corners may not work
...

Example
18 20 15 30 10 14
First Player picks 18, now row of coins is
20 15 30 10 14

Second player picks 20, now row of coins is
15 30 10 14
First Player picks 15, now row of coins is
30 10 14
Second player picks 30, now row of coins is
10 14
First Player picks 14, now row of coins is
10
Second player picks 10, game over
...

So the second player wins
...
The player that makes
the first move can always make sure that the other player is never able to choose an even coin if sum of even coins is
higher
...

Example
18 20 15 30 10 14
Sum of odd coins = 18 + 15 + 10 = 43
Sum of even coins = 20 + 30 + 14 = 64
...
He first
picks 14, now the other player can only pick a coin
(10 or 18)
...


****************************************************************

70) 1000 Coins and 10 Bags
A dealer has 1000 coins and 10 bags
...
How must divide his money into the ten bags?

Solution:
We can represent any decimal number as weights of binary system
...

1 = 2^0
2 = 2^1
3 = 2^0 + 2^1
4 = 2^2
5 = 2^2 + 2^0
6 = 2^2 + 2^1
7 = 2^2 + 2^1 + 2^0
It can be easily generalized
...

Fun is, we can also measure the same using powers of 3
...

Factorization algorithms other than powers of 2 are costly on computer systems
...

Any person working on cryptography can share more details
...


Can you answer following questions?
1
...
Which was the sixth mark and at which position?
Assume that both the players are intelligent enough
...
The sixth mark was X in square 9
...
Hence, the 6th mark must be
placed in a line already containing two of the opponents marks
...

As we know both the players are intelligent enough, the 6th mark could not be O in square 7
...

Hence, the sixth mark must be X placed in square 9
...
Thus O will win the game
Title: Puzzles For Programming Interviews
Description: Puzzles For Programming Interviews . It contains puzzles with easy explanations that are asked in interviews of many IT companies such as Google, Amazon, Microsoft, etc.