Sunday, September 10, 2023

Top 10 Puzzles, Riddles, Logical, and Lateral Thinking Questions for Programming Job Interviews

Puzzles, riddles, logical questions, and lateral thinking questions are an integral part of any programming job interviews. I missed to include some puzzles when I shared my list of top 30 programming interview questions earlier, and a couple of my friends requested to share some puzzles as well. These puzzles and lateral thinking questions are mostly collected from various Java programming interviews on Investment banks, but they are equally useful for any programming job interviews. I have seen more puzzles and logical questions at the junior level i.e. 2 to 4 years.


These kinds of puzzles, if asked at the beginning of the interview, can make or break the interview. It’s not easy to solve them, especially when you see them the first time, but what is surely not to do is give up without a trial, even if you can't think of an answer, try to show a rational approach. Remember it's OK if you don't answer any puzzle question, but your approach towards unknown problem get noticed.

At the same time, lateral thinking questions offers several answers, and you can give your own explanation as well, but make sure it must look genuine and seems logical. 

Puzzles and riddles also form a big chunk of Microsoft and Google Interview questions as well, and they are some really nasty and tough to answer. Anyway, I have not provided answers to these puzzles and logical questions, so that you can at least give it a try. 

Immediate answers kill the purpose of puzzles and riddles, and it's fun to solve them by yourself. By the way, you can easily find answers to these puzzles using Google, as they have asked numerous times on various programming interviews on software and technology companies.




10 Best Puzzles, Riddles, Logical and Lateral thinking question

puzzles, riddles, logical and lateral thinking question programming interviewHere is my list of 10 riddles, puzzles, logical and lateral thinking questions. Try to solve it by yourself before searching for answers in Google, (I have provided answer at the end of the article) they are not that tough or difficult. Easy answers spoil the purpose of puzzles, so don’t look at them, until you are going for an interview the very next day.


1. 27 Coin Problem

There are 27 coins and a two-pan balance. All coins has same weight except for one, which is heavier than all others. All coins looks identical. What is the minimum number of weighing required to certainly find out heavier coin?

2. Two Stick Problem

There are two sticks that take one hour to burn from one end to other. The sticks do not burn at a constant rate i.e. some part may burn faster than others. You need to measure 45 minutes by using these two sticks and a lighter.

3. Railway Tunnel Problem

Two persons are walking into a railway tunnel. Upon crossing 2/3rd of the tunnel, they saw a train is coming from the opposite direction. Each person ran for a different end of tunnel with the speed of 10 km per hour. Fortunately, both persons escaped from the tunnel just right time i.e.. at the time train would have run through them. Assuming train was running with a constant speed and both persons reacted immediately, Find out speed of train.

4. Three Switches Problem

There are three switches outside of a closed room to operate three different light bulb inside the room. You can only go into the room once and you need to determine which switch belongs to which bulb. Just to make it clear, room is perfectly locked and its not possible to determine whether lights are on from outside the room.

5. Jar with Marbles Problem

You have 10 Jars filled with marbles. Each marble weighs 10 gm, except one Jar which contains defective marbles which weigh 9 gm. Given a scale to weigh, How do you find a Jar with defective marbles in just one measurement. Here the scale is a modern-day electronic scale to measure weight.

6. Shooting Wife Problem

A person shoots her wife. Then holds her under water for 5 minutes. Finally, he hangs her. But after 10 minutes they both go out together and enjoy a wonderful dinner together. How can this be?

7. Father and Son Accident Problem

A father and son are going in a car and suddenly their car met an accident. Father died on the spot but the child was rushed to the hospital. When he arrives in the hospital, the doctor says, "I can't operate on this child, he is my son!" How can this be?

8. 5 Liter and 3 Liter Jug Problem

You have a 5-liter Jug and a 3-liter Jug and an unlimited supply of water. You need to measure exactly 4 liters of water but there is no measuring instrument or cup. Also, Jugs are oddly shaped and don't contain any marks.

9. Crossing the bridge Problem

There are Four people who need to cross the bridge during the night. The bridge can hold the weight of two people at a time and can not be crossed without a torch. Each person walks at a different speed, first can cross the bridge in 1 minute, second in 2 minutes, third in 5 minutes and fourth take 10 minutes to cross the bridge. If two persons go together, they walk at the speed of the slower person. What is the least amount in which all four people cross the bridge?

10. Father and Son Age problem

A person's age is many days as his father's age in weeks and as many months as his grandfather's age in years. If you combine the age of all three it comes to 120 years. What is the age of all three?


Puzzle and Riddle Solutions

Here are the solution of all these question I have shared above: 

Solution of 27 Coin Problem

To definitively identify the one heavier coin among 27 indistinguishable coins using only a two-pan balance, a strategic approach can achieve this in just three weighings. Firstly, split the 27 coins into three groups of 9 each and weigh any two groups. 

If they balance, the heavier coin resides within the remaining unweighted group of 9. If one of the groups tips the balance, it narrows the possibilities down to 9 coins. Proceed by dividing this group into three smaller sets of 3 coins each, and weigh two of these sets. 

If they balance, the heavier coin is in the remaining set of 3. Alternatively, if one set is heavier, the heavier coin within the 3 is now identified, having used only three weighings to reach a certain conclusion.


Solution of Two Stick Problem

To measure exactly 45 minutes using the two non-uniform burning sticks and a lighter, follow these steps:

  1. Light one end of the first stick and both ends of the second stick simultaneously.

  2. Simultaneously start a timer.

  3. Once the second stick (which was lit at both ends) completely burns out, which should take 30 minutes since it burns twice as fast, 30 minutes will have passed.

  4. At this point, there will be 30 minutes remaining on the first stick.

  5. As soon as the second stick burns out, light one end of the first stick.

  6. When the first stick (which was relit) completely burns out, which will take 15 more minutes, a total of 45 minutes will have passed.

By using these two sticks and the described method, you can accurately measure 45 minutes.


That's all on this list of top 10 puzzles, riddles, logical questions, and lateral thinking questions asked in programmer interviews. These questions have maximum impact when the programmer doesn't hear about them, and it means they may appear differently in the actual interview. 

As a candidate, you can prepare all these questions to get your mind working and knowing some techniques to solve them but be prepared for surprise in the actual Interview. The key of solving puzzles, logical questions, or riddles is keeping your cool and not panicking.

Even if you don't know the answer you should start explaining your way and logic and if you have any answer which is out of the box then better say it. Many times it happens when candidates are selected even if they don't solve the puzzles but in the process show some promise.


48 comments :

kapil tomer said...

In Question6, Is he a Photographer?

Javin @ String to Integer Java said...

@Kapil, that's correct mate. Good answer. He is photographer, here shot means taking photos :)

treaz said...

@Jarvin why don't you make another blog post with just the answers? I couldn't find the answer for question 3 anywhere...

Anonymous said...

tRain -> [A, B, C] <- tunel
A = B = C, A+B+C = 3A = 3B = 3C

Time T0 = 0
persons P1 and P2 are between A, B, tRain is far away.
Time T1 = t
Person P1 is out of the tunel right in time. tRain is at the start of the tunel. Person P2 is between B and C
Time T2 = 2t
Person P2 made it out of the tunle right in time. tRain is at the end of the tunel.
S = V*T (s = length, v = speed, T = time)
For person P1
A = 10*T
for Person P2
B = 10*t <==> C = 10*t <==> A = 10*t
B + C = 10*2t <==> 2A = 10*2t <==> A = 10t
for Train
A + B + C = x*t <==> 3A = x*t

From P1 or from p1 we can get
A = 10*t
t = A/10
And this we will put in equation of train
3A = x*(A/10)
3A = xA/10
3 = x / 10
30 = x
x = 30 km/h

Anonymous said...

What is the answer for question 4 and 5?

Anonymous said...

Answers
1. require 3 weighing
step 1 take 9 each on each side of balance
eliminate the two sets ;remaining 9
step 2 take 3 each on each side of balance
eliminate the two sets; remaining 3
step 3 take 1 each on each side of balance
eliminate the two sets

2. burn 1st stick from both sides and second from one side. When 1st stick finishes(half an hr) ,ignite the other end of second stick.

3.

4. switch on switch one and two. then turn two off. After entering the room, one bulb will be on(switch one). one bulb will be hot and off(switch 2) and the third will be off and cool

5.take one marble from jar one, two from jar two, three from jar three etc... and weigh.
total weight if all jar contained 10 g each should be 550. Subtract the actual weight from 550, and that will be the index of the jar(eg, if 5th jar contain 9 g marbles, then total weight would be 545; 550-545=5(5th jar)

6.

7. the Doctor is the mother of the boy

8.fill the three-litter jug with water and pour it into the five-litter jug. Repeat. one litter will be left over in the three-litter jug. Empty out the five-littr jug and pour in the one litter. Now just fill the three-litter jug again and pour it into the five-litter jug. . (Mathematically, this can be represented 3 + 3 - 5 + 3 = 4)

9. 17 minutes
A(1) and B(2) cross the bridge, takes 2 mins
B comes back takes 2 mins(total 4)
C(5) and D(10) cross the bridge takes 10 mins (total 14)
A comes back take 1 min (total 15)
A and B cross the brodge again takes 2 mins (total 17 mins)

Rakesh said...

Answer to Question 1 : Key here is to understand that 27 is cube of 3, which means it will take minimum 3 weighing attempts to find defective coin. Let's assume defective coin is heavier than normal coins, now divide 27 coins into group of 9. Out of three groups, weigh two of them in balance. The group with heavier coin, will go down, which means you can discard two groups right that time. Now further divide those 9 coins into 3 groups of 3 coins, and repeat the process, this will give you a group of 3 coins, now divide them into group of 1 coins, at this time (which is third weighing attempt), you will find out exactly which coin is defective. This puzzle can be generalized further, e.g. if interviewer gives you 8 coins or 64 coins

Rakesh said...

@Anonymous, Answer of puzzle 4 is very easy. You can keep one bulb on for couple of minutes and then switch that off and on another switch. Now you can go into room, the one, which is hot is associated with previous switch you pressed, current bulb is the switch you just pressed and the remaining third one is, well you know.

Anonymous said...

Please correct me if I am wrong.

For the third question, I think the speed of train is 30 km/h.

At the moment when train enters the tunnel, the first runner runned 1/3 of the tunnel and safely escaped. The second runner runned 1/3 of tunnel in oposite direction, so he has another 1/3 of tunnel to run when the train has complete tunnel length to pass. The time is equal, so we have t1=t2, meaning third of tunnel lenght devidid by 10km/h is equal as complete tunnel lenght devided by the train speed. That means train speed is 3 times more than 10km/h

Suchit said...

Answer of Questions 8:

1) Completely fill 5 liter Jug with water.
2) Pour water from 5 litter Jug to 3 liter Jug, which means you will left with 2L of water.
3) Now empty 3L jug and pour 2L into them. Now your 3L jug contains 2L of water and 5L jug is empty.

4) Now refill 5L jug and pour into 3L jug which contains 2L water already. Once you fill 3L jug, you will left with 4L of water in 5L jug.

Ahh, lots of juggling but I think this answer the puzzle number 8, anyone has better answers, I mean less steps?

Puneet said...

Answers to 10. 6 years, 42 years and 72 years. classic Solve the equations with three variables problem...

Anonymous said...

answer for 7th is.....

doctor is childs mother.

Ayush Chowdhary said...

answer for 6 is

He is taking and developing a photograph of his wife.

Anonymous said...

answer to first question is 1.
They asked the minimun no of measures. Keep one aside and weigh 13 each on two pans. If they balance out, you have the answer

Anonymous said...

fill up the 5 liter jug and 3 litre jug to its half portion..that is 2.5 + 1.5=4litres ...thats it

Anonymous said...

ya 30kmph is correct for quest 3...

gns

Unknown said...

Fill the 3lit jug and pour water into 5 liter.again fill 3lt and pour water into 5 liter.
So 1 lt is left in 3lt.

Throw the water from 5lt.and fill the 1lt water in 5lt.now again fill 3lt and pour in 5 lt.
1+3=4...done

Unknown said...

ans 3; train speed is 30 kmph
v=d/t first u have to caliculate the time to travel from the 1/3 of the tunnel to one person who travelled towards to train is 10=(1/3)/t from this equation u will get t value is 1/30 hrs and now should go the second person who is the running away from the train.(1/v)=(1/3)/10 from this equation u will get the v value means train speed is 30 kmph

Unknown said...

answer of the 3 one is 30 kmph

Anonymous said...

Q6 says a person shooted HER wife. Then later , it says HE hang her. So i think that the he and her are two different people and the two of them went for dinner. This is just my opinion.

Anonymous said...

Number 9 should be 12 mins.
first A(1) and B(2) cross the bridge in 2 mins
then C(5) and D(10) cross the bridge in 10 mins.
Nobody said there was only one torch.

rajiv said...

answer of question4

first switch on any switch from those 3 and keep it ON for 15 mins and then switch it off . Now switch on any other switch and keep it ON ,Now you enter the room .Touch any one of the bulbs ,if any of those are hot means it belongs to the first switch you turned on and then off . The second bulb is already ON so you know the switch implicitly. The third bulb was always off so you know its switch.

Unknown said...

Ans. of Que 10 .
child's age 6 years
father's age 42 years
grand father's age 72 years

Anonymous said...

This does not proof that three is the minimum, only that it is possible in three times, so it does not solve the problem

Anonymous said...

Answer to Puzzle 7
The Father from Church travels with a son who is not his biological son!

Unknown said...

Answer to Puzzle 7
May be the child and father hit a church father.And may be that doctor is actual mother of the church father.

Anonymous said...

Answer to Question No 3:
Let Length of tunnel = x
When they saw the train, distances to the ends of tunnel: x/3 and 2x/3
Time taken (TT) by 2x/3 person = TT by train to reach the first end + TT by the train inside the tunnel
Time taken (TT) by 2x/3 person = TT by x/3 person + TT by the train inside the tunnel
ie (2x/3)/10 = (x/3)/10 + x/Y (time = distance/velocity: Y = train speed)
2/30 = 1/30 + 1/Y
Y = 30 Km/Hr

Anonymous said...

Answer to Qustion No 1 is 4
1. 13, 13. Remaining 1
2. 6, 6. Remaining 1
3. 3,3. Remaining 0
4. 1,1. Remaining 1

Anonymous said...

Answer to Question No 4. Switch on first switch. Wait for 2 minutes. Switch on second switch. Go inside the room and touch the bulbs to find out which bulb belongs to which switch.

Anonymous said...

Answer to question 7. The doctor is the father of the dead person. the dead person's son brings him to the hospital immediately after the accident. Grand father is the doctor, father is the dead person. His son remains alive.

Anonymous said...

in ques. 9 ans is 12 min
A[1] AND B[2] IT WILL TAKE 2 MIN
C[5] AND D[10] IT WILL TAKE 10 MIN
TOTAL 12 MIN

Anonymous said...

answer for q no. 1 is 3 attempts, we can find the odd coin..
9 & 9 keep 9 on other side,
if blnc take 9 frm other side, balance 3& 3 keep 3 aside,
if balance take 3 frm other side , balance 1 & 1,keep 1 aside.
now u got the odd one out..

Anonymous said...

Q7
Ans:- The person in the car is Church Father :)

Unknown said...

Ans to question 8
1)fill up the 3 lit jug and pour it into the empty 5 lit jug
2)now again fill up the emptied 3 lit jug and pour it to the earlier filled 5 lit jug
3)so now you have 1 L remaining in the 3 lit jug
4)now empty the 5 lit jug and pour the 3lit jug containing 1l water
5)now again fill up the 3 lit jug and pour it into the 5 lit jug already containing 1 lit of water.
6)now it finally adds upto 4 lit

Raja Sekhar Chaliki said...

Q3) person in the car is grand father

Unknown said...

Q6) It is a picture of his wife,he did a photoshoot of her,developed the pic by keeping it under water and then hung the picture for display and later went out with her for dinner.

Anonymous said...

Q5 is quite easy (more elegant solution than previously proposed)
As already said, we take "n" marbles from jar "#n" (1 from jar 1, 2 from jar 2, ..)
If you take a look at the multiplication table for 9, you will notice a nice property
for the last digit:
1x9 = 9
2x9 = 18
3x9 = 27
4x9 = 36
5x9 = 45
6x9 = 54
7x9 = 63
8x9 = 72
9x9 = 81
10x9 = 90
The last digit is inverse to the factor:
3x9 = 27 --> 3 (factor) = 10 (inverse to) - 7 (last digit)
And since any sum of any multiples of 10 (20 = 2*10, 30 = 3*10, ..) wont
change the last digit, we can assume that the jar with the 9g marbles is:
10 -
jar 1 yields 9
jar 2 yields 8
jar 3 yields 7 and so on..
(Programmatically the last digit is just the result of mod 10)

Anonymous said...

They're irregularly shaped.

Anonymous said...

1. 9,9. Remaining 9
2. 3,3. Remaining 3
3. 1,1. Remaining 1

Unknown said...

Yes, finally someone who doesn't think it's 3. It can be done another way too though. Take 2 coins and put them on the scales. If 1 side is heavier, you have yourself an answer!!!

Domingos Tavares said...

Q1 I thinked about binary search and reached 4 tests. But it can find the heavier coin at 1st test.

Test 1:
Put 2 sets of 13 coins each pan.
If set1 = set 2, the coin that's left out is the heavier.
Else eliminate 14 coins (the lighter set and the coin tha's left out). The heavier set is X.

Test 2: divide the set X in 2 sets of 6 coins each and test w/one coin left out.
If set X.1 = set X.2, the coin that's left out is the heavier.
Else eliminate 7 coins (the lighter set and the coin tha's left out). The heavier set is now Y.

Test 3: divide the set Y in 2 sets of 3 coins each and test w/one coin left out.
If set Y.1 = set Y.2, the coin that's left out is the heavier.
Else eliminate 4 coins (the lighter set and the coin tha's left out). The heavier set is now Z.

Test 4: test 2 coins of set Z.
If coin Z.1 = coin Z.2, the coin that's left out (Z.3) is the heavier.

Anonymous said...

I was sure I had 6 but now I feel like an idiot.

"A person shoots HER wife. Then holds her under water for 5 minutes. Finally, HE hangs her"

I thought the typo with her instead of his was intentional to suggest that it was a married lesbian couple. One woman actually shot and drowned her wife, then HE (the man she was having an affair with) came in and hanged her. Then those two went out to dinner after finishing the job.

Wow. I'm glad that was not my interview question.

khajan said...

In the best case the minimum number of weighing is 1, 13/13 if scales equal remaining coin is defective/heavier/lighter

Anonymous said...

6. The person shoots with camera, holds the photo into the water,hangs it and then went for dinner with his wife.

Anonymous said...

1) use bubble search

Unknown said...

number 8
#1) 3 litters 2 times to 5 litters
#2) 3 litters will get 1 litter.
#3) 3 litters's 1 litter move to 5 litters.
#4) full 3 litter move to 5 litters, and (1+3) is 4

Anonymous said...

3 is the minimum attempt that guarantee the solution... otherwise it could be just random picking that doesn't guarantee u will be right

Karan said...

1st one is wrong

Post a Comment